Which exact driver is sqlalchemy using? - sql-server

I am having trouble with a MS SQL connection when using pyinstaller. When run in interactive mode, everything works as expected. After compiling to an exe, the MS SQL database connection times out on the first query with the following error:
(pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expire (0); ...
My connection string is similar to the following:
create_engine(
"mssql+pyodbc://USER:PASSWORD#SERVERIP/DB_NAME?driver=ODBC+Driver+17+for+SQL+Server"
)
In attempting to diagnose the issue, I am printing out the drivers available to pyodbc with pyodbc.drivers() (which shows a large disparity between available drivers in compiled vs interactive) as well as the driver in use using
print(session.bind.dialect.name)
> pyodbc
print(session.bind.dialect.driver)
> mssql
It returns the upper level python modules which are being used but not the .dll that is handling it at a lower level. Is there any way to find which exact driver is being used? Any tips on what could be causing the error in the compiled version in the firstplace would be appreciated as well.

The issue may be in your connection string.
To create a proper connection string to connect to MSSQL Server ODBC driver with sqlAlchemy use the following:
import urllib
from sqlalchemy import create_engine
server = 'serverName\instanceName,port' # to specify an alternate port
database = 'mydb'
username = 'myusername'
password = 'mypassword'
params = urllib.parse.quote_plus('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
Also, you can check the following article Connecting to Microsoft SQL Server using SQLAlchemy and PyODBC

Is there any way to find which exact driver [.dll] is being used?
import pyodbc
cnxn = engine.raw_connection()
print(cnxn.getinfo(pyodbc.SQL_DRIVER_NAME)) # msodbcsql17.dll
print(cnxn.getinfo(pyodbc.SQL_DRIVER_VER)) # 17.08.0001

Related

pyodbc - Sqlalchemy Connection failed

Need help using a trusted connection to connect to SQL Server using SQLALCHEMY
So far I have this code to establish the connection
import sqlalchemy as sa
engine = sa.create_engine("mssql+pyodbc://REPORTING/REPORTING_System?driver={SQL Server}?TrustedConnection=yes")
view = f"SELECT Date From Table"
df = pd.read_sql_query(view, engine)
However I got this error message - DBAPIError: (pyodbc.Error) ('01S00', '[01S00] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/dbapi)
I tried to change the driver to {ODBC Driver 17 for SQL Server}
It did not help.

How do I get past this certificate error when I connect to SQL Server from Python / Debian Bullseye? [duplicate]

I'm trying to connect to a SQL server database using pyodbc in Python 3. But I get an error when I'm trying to establish the connection.
I do something like this:
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;')
And I get this:
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol][error:140B40C7:SSL routines:SSL_do_handshake:peer did not return a certificate] (-1) (SQLDriverConnect)')
Does anybody know how to solve this? The database is not my own, so I hope there is a solution that doesn't require changing any settings there.
I'm running Ubuntu within the Windows Subsystem for Linux.
There is a breaking change in ODBC Driver 18 for SQL Server
Similar to the HTTP to HTTPS default changes made in web browsers a few years back (and the security reasons for them), we are changing the default value of the Encrypt connection option from no to yes/mandatory.
ODBC Driver 18.0 for SQL Server Released
So this
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;')
is the same as
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=yes')
If you don't want an encrypted connection you must opt out:
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=no')
We also changed the behavior of TrustServerCertificate to not be tied to the Encrypt setting
So if your server is using a self-signed certificate, you also must opt out of certificate validation. so
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=no;TrustServerCertificate=yes')
I ended up taking my script out of WSL. Running the same command (with David's additions or ODBC Driver 17 for SQL Server instead of 18) under Windows works without issues in my case.

Issues connecting to Microsoft SQL Server Express using pyodbc on a Ubuntu 20.10 machine [duplicate]

I am trying to connect to MS SQL Server using pyodbc from a remote machine running Ubuntu 16.04.
import pyodbc
conn = pyodbc.connect(r'DRIVER=ODBC Driver 17 for SQL Server; SERVER=xxxTest-SRV; PORT=51333; DATABASE=TestDB; UID=xxxx; PWD=xxxx;')
I'm getting following error:
pyodbc.OperationalError: ('HYT00', '[HYT00] [unixODBC][Microsoft][ODBC
Driver 17 for SQL Server]Login timeout expired (0)
(SQLDriverConnect)')
I tried using the server IP in the connection string but still no luck.
However, I am able to connect to using sqlcmd from the terminal
Following works:
sqlcmd -S xxxTest-SRV, 51333 -d TestDB -U xxxx -P xxxx
I didn't find any issue that gave an answer to my problem.
odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-17.1.so.1.1
UsageCount=1
There's always seems to be an issue connecting to MS SQL Server using pyodbc from a linux machine. Is there a way to connect to SQL Server from Python. I'll appreciate your help in solving this error. Thank you.
[UPDATE]
As per the below answer, I updated the connection string. But, now I get following error:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open
lib '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1' : file
not found (0) (SQLDriverConnect)")
My odbcinst.ini file driver definition:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1
UsageCount=1
It has always been a nightmare to connect to MS SQL Server from a Linux machine. Can you please tell which pyodbc, unixODBC and Driver version is the most stable?
I have installed the driver following this Microsoft instructions. My pyodbc version is 4.0.23
Microsoft's ODBC drivers for SQL Server do not use a PORT= parameter. The port number, if any, is appended to the server name/IP with a comma, e.g.,
SERVER=xxxTest-SRV,51333;
I ran into the same kind of issue, but my scenario is connecting to SQL server hosted on ec2 instance through AWS Lambda function using PyOdbc module. For me, replacing the host name with IP address of ec2 instance fixed it.
I found out that it was not able to resolve dns. So if any of the above steps didn't work for you, please try using the ip address and comment here
I ran into same problem when working with SQL Server docker container inside VS Code development container. Finding the IP of SQL Server container (credits for that goes to freecodecamp.org How to get docker container IP address) and replacing server name with IP solved the problem
conn = pyodbc.connect(
"DRIVER={ODBC Driver 17 for SQL Server};" + "SERVER=172.xx.x.x;"
"DATABASE=xxx;"
"UID=sa;"
"PWD=xxx;")
What solved in my case was to configure in the "SQL Configuration Manager" --> SQL Server Network Configuration --> Protocols for XYX --> TCP/IP --> Properties --> IP Addresses --> IPAII (Here set empty in TCP Dynamic Ports and 1433 (or the port that you want to use) at TCP Port)
You can have more details here
Microsoft's ODBC drivers for Linux cannot resolve instance names, so this won't work from a Linux client:
Server = mydbserver.mycompany.com\SQLEXPRESS
If you need to connect to a named instance you can use the (free) sqlserverport (https://github.com/gordthompson/sqlserverport) module.
import pyodbc
import sqlserverport
servername = 'myserver'
serverspec = '{0},{1}'.format(
servername,
sqlserverport.lookup(servername, 'SQLEXPRESS'))
conn = pyodbc.connect('DRIVER=ODBC Driver 17 for SQL Server;SERVER={};...'.format(serverspec))

Could not connect newly created SQL Server database using pyodbc

I have configured a new SQL Server 2017.XXX instance in my Windows 10. Then, created a new database called CUSTOMER_DEVICES. I tried to connect to database using pyodbc. I could not and got below error:
Error - pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
When I use the same code in existing production server it works. Code below
import pyodbc
conn = pyodbc.connect(r'Driver={SQL Server};Server=localhost;Database=CUSTOMER_DEVICES;Trusted_Connection=yes;')
cursor = conn.cursor()
print(cursor)
Check if SQL server Configuration Management is configured. Enable TCP, IP. Check below link Check here

Unable to connect to MS SQL Server using pyodbc on Scintific Linux 7.5

My OS is Scientific Linux 7.5 and wants to connect to MS SQL server using python 2.7.5 i have configured unixODBC 2.3.1 and Microsoft ODBC Driver 13 for SQL Server
But when i use the following code:
import pyodbc
cnxn = pyodbc.connect(
'Driver={/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2};'
'Server=localhost;'
'User=sa;'
'Password=xxx;'
'Database=yyy;'
'Trusted_Connection=yes;')
i get the following error
pyodbc.Error: ('HY000', u'[HY000] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]SSPI Provider: No Kerberos credentials available (default cache: KEYRING:persistent:1000) (851968) (SQLDriverConnect)')
and got the same error when used 'Driver={ODBC Driver 13 for SQL Server};'
if i use 'Driver={SQL Server};' i got stuck on following error
pyodbc.Error: ('01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
You should remove 'Trusted_Connection=yes;' from your connection string. It is used for Windows authentication, while you are trying to connect with SQL authentication (providing user name and password).
An alternate cause and solution, in case anyone ends up here for this reason:
If you're relying on an env file to populate the Username and Password components of the pyodbc connection string, and something goes wrong with your execution such that those values are not accessible, then you will get that No Kerberos credentials available error message.
Check the value of the connection string; in my case, the username and password were missing from the SQLAlchemy engine string when I printed it. Running my docker container with the proper --env-file= flag addressed this case.

Resources