Connect to SQLServer with Pyodbc using Trusted Connection on OSX - python

Is it possible connect using local Domain credentials that I use to log into my OSX System to connect to a SQLServer DB with python, pyodbc, unixodbc, and FreeTDS like I would when connecting from a Windows system?
Windows Connection String that works:
pyodbc.connect('Trusted_Connection=yes', driver='FreeTDS', TDS_Version = 7.0, server='<SERVER>', port=<PORT>, database='<DATABASE>')
When I attempt this from a Mac I get the following error:
pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
I am attempting to avoid hard coding usernames and passwords.

The Trusted_Connection setting indicates whether to use Windows Authentication Mode for login validation or not. Given that you're using a Mac, I suspect that Windows Authentication Mode will not be possible and so it will be necessary to pass the username and password to the connection string.

Related

Connect to Tally ODBC from Python from other network

I am not able to connect to tally odbc from another network. I have whitelisted my IP and Opened port 9000. Tally is installed on a machine in cloud. Here is what I am doing:
import pyodbc
conn = pyodbc.connect('Dsn=TallyODBC64_9000;Driver={Tally ODBC Driver};server=20.xx.xx.xx;port=9000')
It is giving me this error:
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I have looked on the internet but couldn't find anything on connecting outside of your network.
Questions:
Do I need to install Tally ODBC Driver on my machine?
Can you guide me on where I am going wrong.
Thanks.
Try Accessing your server in browser http://20.xx.xx.xx:9000
if it shows <RESPONSE>TallyPrime Server is Running</RESPONSE>
then try connecting using ODBC
if its shows nothing, then you need to check whether Port configured in Tally is allowed in Cloud security setting and system firewall settings

Pyodbc login time out error with (Azure) SQL Server

I am getting a strange error with Python condo environments and PyODBC. I have multiple Conda environments, I am able to connect from one of the environment (from a Linux machine) to SQL Server hosted on Azure but not from the other one. Both Python environments have version 3.7.7 of Python and version 4.0.0 of Pyodbc. Code is exactly the same and connection string uses SQL Server 17 driver.
conn_str='DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +server+';Authentication=ActiveDirectoryPassword;DATABASE='+database+';UID='+self.user_name+';PWD='+self.password
Error is :
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
As you can see above, the issue might be DNS-related. In the
connection string, try using an IP address instead of the hostname,
or check your DNS setup.
Try downgrade to ODBC driver 13 and have a try. For that version 17
needs to be uninstalled.
Try using connect from SQL Authentication instead of Active Directory
Password.
The SQL Server ODBC drivers for Linux from Microsoft are unable to determine instance names.

Issues connecting to a DB2 server using Python. Possible issue identifying the driver

I had written code to connect to a DB2 server using Python that was working fine until my company had me upgrade the IBM DB2 Driver to 11.5. Now, its no longer working and I'm getting the following error message:
SQLCODE=-30082n: [IBM][CLI Driver] SQL30082N Security processing failed with reason "15" ("PROCESSING FAILURE"). SQLSTATE=08001
I verified that I have the right password. Also, I confirmed that I still have access to the Database by going through the ODBC Manager in Windows and testing a connection that way. I've checked several pages here on Stackoverflow and other sites and nothing seems to work. Most of the issues people are having on this site involve different error messages.
Here is the original code using the ibm_db module:
import ibm_db
import ibm_db_dbi
con = ibm_db_dbi.Connection(ibm_db.connect("ABC1011","rsmith","Passw123"))
When that stopped working i tried connecting using pyodbc as a connection:
import pyodbc
cnxn = pyodbc.connect('DRIVER={IBM DB2 ODBC DRIVER - DB2COPY2};Database=ABC1011;UID=rsmith; PWD=Passw123')
Again, I already verified in the ODBC Datasource Administrator (64-bit) that I am using the correct password, I have the correct database, and that I am using the correct driver's name. Does anyone know what else I may need to include to get a connection to work?
Thank you.

Unable to connect to SQL Server with Windows Authentication in a Python script on a MAC

I am trying to run a phyton script on my Mac (OS Sierra) to connect to SQL Server (2016) using my login which is a Windows AD authentication but can't seem to get it to work. It throwing an Image not found error message. However, the pytds and pyodbc module both work with another login that uses Sql Server authentication.
I am unable to even trace where the disconnect is. Can someone please advice?
Try this connection string:
conn= pyodbc.connect(r'Driver={SQL
Server};Server=yourServer;Database=yourDatabase;Trusted_Connection=yes;')
replace yourServer and yourDatabase

Azure SQL Server and FreeTDS ODBC with linux and windows dev environments

I host a web app on Windows Azure (Flask), which runs on Windows Server.
I have a linux development environment (some other contributors may use windows in the future), so we use pyodbc to communicate with the SQL Server. Unfortunately, I have to change my connection string every time I pull or push from the Azure deployment due to linux and windows ODBC connection differences.
Consider my connection string:
pyodbc.connect('Driver=SQL
Server;Server=tcp:mydbname.database.windows.net,1433;Database=mydbname;Uid=dbuser#mydbname;Pwd=topsecret;Encrypt=yes;TrustServerCertificate=no;Connection
Timeout=30;')
This works fine once it's deployed on the server (Windows Server), or on the windows box, but doesn't work on the linux dev box. I found some over SO questions and a guide on how to setup FreeTDS (e.g. Connecting to Microsoft SQL Server through pyODBC on Ubuntu and http://www.gazoakley.com/content/connecting-sql-azure-python-ubuntu-using-freetds-and-unixodbc, http://blog.tryolabs.com/2012/06/25/connecting-sql-server-database-python-under-ubuntu/).
However, after following those guides through you have to remove the server name and replace it with a the DSN (data source name), which is a locally configured variable. While that will work on a local linux box, that doesn't work once it's deployed to the Azure web server (windows).
pyodbc.connect('DSN=SQL Server;Driver=SQL
Server;Database=mydbname;Uid=dbuser#mydbname;Pwd=topsecret;Encrypt=yes;TrustServerCertificate=no;Connection
Timeout=30;')
You have to remove the servername otherwise it conflicts with the odbc/FreeTDS config.
With no DNS specified (first connection string in this post):
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source
name not found, and no default driver specified (0) (SQLDriverConnect)
With DNS and servername:
pyodbc.Error: ('HY000', '[HY000] [unixODBC][FreeTDS][SQL Server]Only
one between SERVER, SERVERNAME and DSN can be specified (0)
(SQLDriverConnect)')
With DNS and no server name
When deployed to the server it wont work.
We use git for version control, and I don't want to have continuous merge conflicts as developers change their connection string to match their dev enviroments and/or deploy to the Azure site.
Is there a clean fix for this. Gut tells me I'm doing something wrong, and this is very trivial.
First of all, if you have successfully connected to SQL Server both in Windows and Linux platforms, congratulations. In python, there is a package installed by python default named ‘platform’, we can use it to check the what platform python script run in. And we can set 2 connection string in advance, and select the specific string in different platform.
Here is my python code snippet:
import platform
def getOBCDString():
stsos = platform.system()
bol = False
odbcstring=''
if(stsos == "Windows"):
bol = True
odbcstring = 'windows_odbc_string'
elif(stsos == "Linux"):
bol = True
odbcstring = 'Linux_odbc_string'
#else:
# custom error handle
return (bol,odbcstring)
bol,string = getOBCDString()
if(bol):
print(string)

Categories