I am on a Centos 7 Linux machine trying to connect to an SQL database through pyodbc. I learned that you need to setup the DSN and you do that by installing the freetds driv
To build on @FlipperPA's answer, it's not obvious how pyodbc "finds" the FreeTDS driver. If you have this error:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found (0) (SQLDriverConnect)")
There can be other possible causes, including an incorrect environment. Here's what I discovered:
pyodbc is a wrapper around unixODBC, which isn't documented, but you need to install the unixODBC devel packages before you can pip install pyodbc
. pyodbc passes the connection string directly to unixODBC.
unixODBC needs to load a shared library containing the ODBC database driver, for example libtdsodbc.so
for FreeTDS. You can set the DRIVER
parameter in the connect string to one of two things:
/usr/local/lib/libtdsodbc.so
)odbcinst.ini
, which contains a Driver = ...
setting that points to the shared library fileThe first way is guaranteed to find the shared library, and a good way to check whether you have environment issues, but the second way is preferred and more portable. See here for more details:
This ini file simply lists all installed drivers. It is located in /etc/odbcinst.ini. The syntax is simple; a name followed by a property which tells us the drivers file name. For example;
[Sybase 11] Comment = Super Duper Sybase Server Driver = /usr/lib/libsybase.so.11 Setup = /usr/lib/libsybaseS.so.11 FileUsage = 1
The Driver file name (ie
/usr/lib/libsybase.so.11
) should be unique. The friendly name (ieSybase 11
) must also be unique.
However, this can only work if unixODBC can find your odbcinst.ini
file. It appears to search for it:
.odbcinst.ini
ODBCSYSINI
environment variable, if set./etc
.For FreeTDS it should contain something like this:
[FreeTDS]
Description = For example, my database server name or FreeTDS version
Driver = /usr/local/lib/libtdsodbc.so
Only then can you use DRIVER=FreeTDS
in a connect string and expect it to work (and not get the above error).
You might also want to use the ldd
command (on Linux) to check that all of the library's dependencies are satisfied, and can be found and loaded by the dynamic library loader, ld.so
:
ldd /usr/local/lib/libtdsodbc.so
ldd: warning: you do not have execution permission for `/usr/local/lib/libtdsodbc.so'
linux-vdso.so.1 => (0x00007ffe145fe000)
libodbcinst.so.2 => /lib64/libodbcinst.so.2 (0x00007f81f9dfd000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f81f9bf8000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f81f99dc000)
libc.so.6 => /lib64/libc.so.6 (0x00007f81f961b000)
libltdl.so.7 => /usr/local/lib/libltdl.so.7 (0x00007f81f940f000)
/lib64/ld-linux-x86-64.so.2 (0x00007f81fa2ac000)
If you are still stuck, you might want to start over from scratch by following this or this answer.
If you're compiling FreeTDS from source, it'll install to /usr/local/freetds, IIRC. You can also install via yum
on CentOS, and you'll need unixODBC as well. Basically, FreeTDS bridges SQL Server to unixODBC, and pyodbc bridges unixODBC to Python.
Here's an example set up with FreeTDS, unixODBC, and friends:
freetds.conf:
[server]
host = server.com
port = 1433
tds version = 7.3
odbc.ini:
[server]
Driver = FreeTDS
Server = server.com
Port = 1433
TDS_Version = 7.3
odbcinst.ini:
[FreeTDS]
Description = FreeTDS with Protocol up to 7.3
Driver = /usr/lib64/libtdsodbc.so.0
The Driver =
location may differ above, depending on your distro of FreeTDS - if you compiled from source, most likely, /usr/local/freetds/lib/libtdsodbc.so
.
pyodbc connect, DSN free:
DRIVER={FreeTDS};SERVER=server.com;PORT=1433;DATABASE=dbname;UID=dbuser;PWD=dbpassword;TDS_Version=7.3;
A few notes:
See here for more:
https://msdn.microsoft.com/en-us/library/dd339982.aspx
Good luck.