Error connecting to MS SQL Server using pyODBC, unixODBC and FreeTDS (on a Mac)

懵懂的女人 提交于 2019-12-06 06:03:01

There are a lot of moving parts in your question. Not only do you have Notebook-on-Python-on-ODBC, but you have iODBC and OS X. Yikes!

The question boils down to this: Where does iODBC look for odbc.ini? I don't know of an ODBC function that reports that information.

Because it's so doggone complicated, I suggest using OS X dtruss(1). Capture the output to a file, and grep for odbc.ini and/or open commands. Once you know where it's looking, you can put your file there, and just follow directions. ;-)

The reason the osql script doesn't work on OS X is that no one ever cared to make it work, or ever complained about it on the FreeTDS mailing list. The first message is a doozy:

/usr/local/bin/osql: line 53: ldd: command not found

I work around that with

$ command -V ldd
ldd is aliased to `otool -L'

That might help. OTOH, the script was written with unixODBC in mind, because it's so much more popular.

Here's an example I think would work for you. If you're using FreeTDS 0.95, you can use TDS Version 7.3, if you're using 0.82 or less, use 7.1. I've never bothered with testing osql with this stack, if tsql and isql work, you should be able to get the rest working, but the nuances of the configuration and connection are tricky:

freetds.conf:

[MYSERVERNAME]
    host = MYSERVERNAME.host.com
    port = 1433
    tds version = 7.2

odbc.ini:

[MYSERVERNAME]
    Driver = FreeTDS
    Server = MYSERVERNAME.host.com
    Port = 1433
    TDS_Version = 7.2

odbcinst.ini:

[FreeTDS]
    Description = TD Driver (MSSQL)
    Driver = /usr/local/lib/libtdsodbc.so

In Python:

connection = pyodbc.connect(r'DRIVER={FreeTDS};SERVER=MYSERVERNAME.host.com;PORT=1433;DATABASE=Database name;UID=Database Username;PWD=DatabasePasswd;TDS_Version=7.2')

TDS Version 8.0 does not exist. 7.2 is the highest supported in FreeTDS 0.91. See here to explain the confusion: http://www.freetds.org/userguide/choosingtdsprotocol.htm

If you're still having issues, try testing with tsql and isql to test the FreeTDS and unixODBC layers of the connection stack respectively. Good luck!

RJH2

Well, we solved it -- with the help of a lot of people on this page and over here, chasing down a lot of blind alleys.

As (eventually) suspected, it was the pyodbc link in the connection. I was using pyodbc v3.0.10, by downloading from the Anaconda package repository. The solution was v.3.0.9. Once I uninstalled v3.0.10, downloaded v3.0.9 from the pypi repository and then built and installed my own conda package... it worked.

The steps I took were as follows (note these were specific to an anaconda environment):

conda uninstall pyodbc

conda skeleton pypi pyodbc --version 3.0.9

conda build pyodbc

conda install pyodbc=3.0.9 --use-local

Once I went back to my Jupyter notebook and ran the same code above, it created a good connection.

I do not know what is wrong with v.3.0.10, or if it's just the files that anaconda.org has on their repository. I've posted something on the pyodbc github page also, but it doesn't look that active.

Anyway, thank you for everyone's help. I hope this saves someone some time.

Just uninstalling pyodbc and reinstalling it back solved the problem for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!