简体   繁体   中英

Reading table into pandas using sqlalchemy from SQL Server

I've been at this for many hours, and cannot figure out what's wrong with my approach. I'm trying to read a table into pandas using sqlalchemy (from a SQL server 2012 instance) and getting the following error:

DBAPIError: (Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)") None None

I'm using the following code:

import sqlalchemy as sql

from sqlalchemy import create_engine

import pyodbc

pyodbc.connect('DSN=MYDSN;UID=User;PWD=Password')

Which returns:

<pyodbc.Connection at 0x10a26a420>

Which I think is good. Then when I run the following:

connectionString = 'mssql+pyodbc://User:Password@IPAdress/Database'
engine = sql.create_engine(connectionString)

pd.read_sql("ecodata", engine)

I get the following error mentioned above.

Is there something wrong with my driver setup? I've been wrestling with the driver setup for days and thought I had it beat.

Any help is greatly appreciated.

For the record, the answer to my question was figured out by joris:

My syntax for the connection string was wrong, and when I changed it from this

connectionString = 'mssql+pyodbc://User:Password@IPAdress/Database'

to this

connectionString = 'mssql+pyodbc://User:Password@IPAddress:Port/Database?driver=FreeTDS'

It worked!

Thanks again for the help!

Try forming your connection like this. You need a few more parameters.

con = pyodbc.connect('DRIVER={FreeTDS};SERVER="yourserver";PORT=1433;DATABASE=yourdb;UID=youruser;PWD=yourpassword;TDS_Version=7.2;')

To figure out which TDS Version to use:

http://www.freetds.org/userguide/choosingtdsprotocol.htm

Hopefully, this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM