how do I fix this pyodbc Driver syntax error - python

I'm using SQLAlchemy in python on a windows machine to connect to a SQL Server. I'm pretty sure that I am generating the SQL engine correctly, I have:
Engine(mssql+pyodbc://P-CEP-SQL:1433/services_vars?driver={ODBC Driver 18 for SQL Server})
A previous version, connecting to a different copy of the same server works with:
Engine(mssql+pyodbc://cam-tls1:1433/services_vars?driver=SQL Server)
however, with the Driver 18 line, I'm now getting:
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)')
when I remove the {} I get:
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Which I'm pretty sure means that it can't find the driver....
It gives this for any string with { } in it, but I'm pretty sure that that's the correct name of the driver. Do I need to extra escape something?

The correct syntax appears to be:
'DRIVER=ODBC+Driver+17+for+SQL+Server',
This works for version 17, but not 18 for some reason.

Related

How do I figure out the correct DRIVER string for ODBC SQL connection (in python)

I have a python script that is using sqlAlchemy to connect to a SQL Server DB. We moved the DB from a local server in the closet to AWS.
When running from a mac, I have:
'DRIVER=/usr/local/lib/libtdsodbc.so',
and that works for both closet and AWS
on a Windows machine, for the closet the connection contained:
'DRIVER=SQL Server',
which gives the error message:
(pyodbc.Error) ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Cannot generate SSPI context (0) (SQLDriverConnect); [HY000] [Microsoft][ODBC SQL Server Driver]Cannot generate SSPI context (0)')
searching around, tells me that maybe I need a new driver, so:
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows recommends:
'DRIVER={{ODBC Driver 18 for SQL Server}}'
(Because I'm formatting into the connection string, so I need to escape the {})
gives:
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)')
alternatively,
'DRIVER=ODBC Driver 18 for SQL Server',
gives:
(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
This SQLAlchemy engine works:
Engine(mssql+pyodbc://cam-tls1:1433/services_vars?driver=SQL Server)
This one does not:
Engine(mssql+pyodbc://P-CEP-SQL:1433/services_vars?driver={ODBC Driver 18 for SQL Server})
it gives a syntax error on the DRIVER keyword...
Do I need to download a new library? or do I need to specify the driver differently?
I'm still not exactly sure how I was SUPPOSED To find this out, but this works:
'DRIVER=ODBC+Driver+17+for+SQL+Server',
even though 18 appears to be current, it doesn't work for 18. but it does work for 17.

Python pyodbc IM002 database problem connection

I'm trying to connect to a database using pyodbs but I have this error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)').
I can't find a nice procedure to solve this problem, anyone can help me?
This is my code:
import pyodbc
server='tcp:univerity_server_name'
database= 'db_name'
username='us_name'
password='pw'
connectionString='DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+' DATABASE='+database+';UID='+username+';PWD='+password
cnxn=pyodbc.connect(connectionString)
Thanksss

InterfaceError: Data source name not found and no default driver specified (0) (SQLDriverConnect)

This question has been asked before (see link below) but I'm still having issues even though both my Python and MS Office are 64-bit. After ensuring both are 64-bit, I tried running the following code:
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=Q:\Data Requests\Boarding.accdb;')
but still got the following error:
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Also of note, I am able to connect and pull data into Python from my SQL server just fine using this code:
conn = pyodbc.connect('Driver={SQL Server};'
'Server=ENTSNR;'
'Database=EMData;'
'Trusted_Connection=yes;')
PYODBC--Data source name not found and no default driver specified

Python: pyodbc can't connect to SQL Server

I am trying to connect to SQL Server 2008 R2 using pyodbc.
This is my code snippet:
conn_strng = ['Driver={SQL Server Native Client
11},'Database=db_name','uid=user','pwd=password','trusted_source=yes']
cnxn = pyodbc.connect(';'.join(conn_strng))
I get the following error:
('IM002', '[IM002] [Microsoft] [ODBC Driver Manager] Data Source name not found or no default driver specified (0) (SQLDriverConnect)')
I have installed the SQL Server ODBC Driver 11. I am using Windows Sever 2012. I have searched in Stackoverflow and tried by changing the Driver string and other string formatting (i.e. no space etc.) but no luck.
Do I need to specify port as well?
Please help.
Ok. So I have found out a solution. Below is the correct connection string
Driver={SQL Server} #<--Note that it is different than earlier
Database=db_name
uid=user
pwd=password
trusted_source=no #<--if we set this 'yes' then it will try to connect the DB with windows login. This was causing error
I think it is better to use Driver={SQL Server} for a safer side.

"Data source name not found and no default driver specified" when connecting to Teradata with pyodbc on Windows

I am trying to connect to a Teradata database using the following python snippet:
con = pyodbc.connect("Provider = Teradata; DBCNAME = teradata-dev,
database = mydatabase UID = myuserid, PWD = mypassword")
Database, UID, and PWD hold non-dummy values. The data source is defined in "32-bit ODBC administrator"
I get the following error message:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data
source name not found and no default driver specified (0) (SQLDriverConnect)')
I have no problem connecting using R's RODBC packge. What am I doing wrong?
OS: Windows 7 professional 64 bit
Teradata Driver: 32 bit, version 14.10.00.00
Python: 32 bit, Python 2.7.10 | Anaconda 2.3.0
UPDATE:
I've tried a different connection string:
"sConn=ODBC;Driver={Teradata};DSN=teradata-dev;database=mydatabase;
UID=myusername;PWD=mypassword"
and now I get the following error:
pyodbc.Error: ('28000', '[28000] [Teradata][ODBC Teradata Driver] Not
enough information to log on (0) (SQLDriverConnect); [28000]
[Teradata][ODBC Teradata Driver] Not enough information to log on (0)')
UPDATE #2:
I got it to work correctly by using the DSN directly.
"DSN = dsninODBC;USN = myusername;PWD = mypassword"

Categories

Resources