I am getting an error when trying to connect to a MS SQL server in python, using the pyODBC --> unixODBC --> FreeTDS --> MS SQL stack. I’ve spent a lot of time on this, and there are some great resources if you’ve reached this question with more fundamental problems getting a connection to work here and here.
However, my question is regarding an error that (I think) is very close to the finish line of this very frustrating experience. Specifically, this code in jupyter notebook:
pyodbc.connect(
'DRIVER=/usr/local/lib/libtdsodbc.so;'
'SERVER=MyServerIP;'
'PORT=1433;'
'DATABASE= DatabaseName;'
'UID=MyUsername;'
'PWD=MyPassword')
Is giving me this error:
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-7-d6b29b647116> in <module>()
1 pyodbc.connect(
----> 2 'DRIVER = /usr/local/lib/libtdsodbc.so;'
3 'SERVER = MyServerIP;'
4 'PORT = 1433;'
5 'DATABASE = DatabaseName'
Error: ('HY000', '[] (20013) (SQLDriverConnect)’)
And if I replace 'DRIVER=/usr/local/lib/libtdsodbc.so;' with 'DRIVER=FreeTDS;' I get:
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-12-607f0d66e615> in <module>()
1 pyodbc.connect(
----> 2 'DRIVER=FreeTDS;'
3 'SERVER= MyServerIP;'
4 'PORT=1433;'
5 'DATABASE= DatabaseName;'
Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen(FreeTDS, 6): image not found (0) (SQLDriverConnect)')
Which leads me to believe there is something wrong with the unixODBC --> FreeTDS connection because of the reference to iODBC. Said differently, unless I specifically provide a path to the FreeTDS driver, it seems to ignore my odbcinst.ini and odbc.ini files, which reference FreeTDS and its location as my driver (see below)
When running tsql and isql from the terminal, both yield good connections with the server.
However, when I run the osql, I get the following error:
$ osql -S MyServerIP -U MyUsername -P MyPassword
checking shared odbc libraries linked to isql for default directories...
/usr/local/bin/osql: line 53: ldd: command not found
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strings: can't open file: (No such file or directory)
osql: problem: no potential directory strings in "/usr/local/bin/isql"
osql: advice: use "osql -I DIR" where DIR unixODBC\'s install prefix e.g. /usr/local
isql strings are:
checking odbc.ini files
reading /Users/myname/.odbc.ini
[MyServerIP] not found in /Users/myname/.odbc.ini
cannot read "/odbc.ini"
osql: error: unable to locate MyServerIP in any odbc.ini
Background on my setup
My connection was built (and rebuilt and rebuilt) using the two resources linked in the first paragraph and my complete set up looks like this:
Environment
Mac OSX 10.11.5
Microsoft SQL Server 2012 – AWS EC2 instance (cloud)
Anaconda 4.0
Python 3.5.1
Jupyter Notebook 4.1.0
Connection Stack
unixODBC – installed using homebrew
FreeTDS – installed using homebrew with the command: `$ brew install
freetds --with-unixodbc`
pyODBC 3.0.10 – installed using conda install
MS SQL – AWS EC2 instance (cloud)
Reference Files
My freetds.conf file reads like this:
[MYSERVERNAME]
host = MyServerIP
port = 1433
tds version = 7.3
client charset = UTF-8
My odbcinst.ini file reads like this:
[FreeTDS]
Description = TD Driver (MSSQL)
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
FileUsage = 1
My odbc.ini file reads like this:
[MYSERVERNAME]
Driver = FreeTDS
Server = MyServerIP
Port = 1433
I am at a complete loss, having spent far more hours than I should have on this. If anyone has any suggestions, I’d be forever grateful.
Thanks.