Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyODBC : can't open the driver even if it exists

I'm new to the linux world and I want to query a Microsoft SQL Server from Python. I used it on Windows and it was perfectly fine but in Linux it's quite painful.

After some hours, I finally succeed to install the Microsoft ODBC driver on Linux Mint with unixODBC.

Then, I set up an anaconda with python 3 environment.

I then do this :

import pyodbc as odbc

sql_PIM = odbc.connect("Driver={ODBC Driver 13 for SQL Server};Server=XXX;Database=YYY;Trusted_Connection=Yes")

It returns :

('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0' : file not found (0) (SQLDriverConnect)")

The thing I do not undertsand is that PyODBC seems to read the right filepath from odbcinst.ini and still does not work.

I went to "/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0" and the file actually exists !

So why does it tell me that it does not exist ? Here are some possible clues :

  • I'm on a virtual environment
  • I need to have "read" rights because it's a root filepath

I do not know how to solve either of these problems.

Thanks !

like image 267
Joseph Yourine Avatar asked Jan 14 '16 09:01

Joseph Yourine


People also ask

What driver do I use for Pyodbc?

pyODBC uses the Microsoft ODBC driver for SQL Server.

What is the alternative to Pyodbc?

Top Alternatives to pyodbcPython wrapper for the Cloudflare v4 API. The AWS SDK for Python. Powerful data structures for data analysis, time series, and statistics. Pytest: simple powerful testing with Python.

How do I know if ODBC driver is installed?

Open the Windows Control Panel. Open the Administrative Tools folder. Double-click Data Sources (ODBC) to open the ODBC Data Source Administrator window. Click the Drivers tab and locate the SQL Server entry in the list of ODBC drivers to confirm that the driver is installed on your system.


2 Answers

I also had the same problem on Ubuntu 14 after following the microsoft tutorial for SQL Server Linux ODBC Driver.

The file exists and after running an ldd, it showed there were dependencies missing:

/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version GLIBCXX_3.4.20' not found (required by /opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0) /opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: versionCXXABI_1.3.8' not found (required by

after searching for a while I found its because Ubuntu's repo didnt have GLIBCXX on version 3.4.20, it was at 3.4.19.

I then added a repo to Ubuntu, updated it and forced it to upgrade libstdc++6

sudo add-apt-repository ppa:ubuntu-toolchain-r/test 
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libstdc++6

Problem solved, tested with isql:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> 

After that I tried testing using pdo_odbc (PHP), it then gave me the same driver not found error. To solve this I had to create a symbolic link to fix libodbcinst.so.2:

sudo ln -s /usr/lib64/libodbcinst.so.2 /lib/x86_64-linux-gnu/libodbcinst.so.2
like image 109
RBarreto Avatar answered Oct 12 '22 22:10

RBarreto


I had the same problem 'file not found (0) (SQLDriverConnect)' on MAC OS with the following code

cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=myServerIP,1433;DATABASE=myDBName;UID=sa;PWD=dbPassword')

after googling for two days, I cannot fix the issue even modify the freetds.conf, odbcinst.ini and odbc.ini

finally, I found the solution via replacing DRIVER value

cnxn = pyodbc.connect('DRIVER={/usr/local/lib/libmsodbcsql.13.dylib};SERVER=myServerIP,1433;DATABASE=myDBName;UID=sa;PWD=dbPassword')

My dev environment

  • MAC OS El Capitan
  • python 3.6.1 in Anaconda
like image 6
Karen Chang Avatar answered Oct 12 '22 22:10

Karen Chang