This is continuation of my previous 3-part blog post that list out the steps carried out to establish a connection to Sql Server via ODBC on Ubuntu.
- The version of Ubuntu used in this guide is Ubuntu 12.04.2 LTS (Precise Pangoline) 32-bit.
- unixodbc is installed on the Ubuntu system.
- freetds is installed on the Ubuntu system.
- You are trying to connect to Microsoft Sql Server 2008 or later.
- You know how to use a terminal session in Ubuntu.
- pyODBC (latest version is 3.0.6 as of writing)
(downloadable from http://code.google.com/p/pyodbc/downloads/list)
Download source vs apt-get
The apt-get utility in Ubuntu does have a version of pyODBC. (version 2.1.7)
However, it is badly out-of-date (2.1.7 vs 3.0.6) and may not work well with the newer versions of unixODBC and freetds.
This is especially important if you are trying to connect to later versions of Microsoft Sql Server (2008 onwards).
It is recommended that you use the latest versions of unixODBC, freetds and pyODBC when working with the latest Microsoft Sql Server instead of relying on packages in apt-get.
PyODBC Installation Overview
The steps for installing pyODBC are as follows:
- Download the source zip file. (I’m assuming you know how to do this)
- Extract the contents of the package.
- Install the contents of the package.
- Configure your system environment variables.
Extracting contents from pyODBC zip file
1) Assuming you downloaded the pyODBC zip into the Downloads folder of your Home directory, you should see a screen like the below on your file browser.
2) To extract the contents of the package, right-click on it. A popup menu should appear.
Select the menu item “Extract Here”.
3) After you extracted the contents, your file browser should look like the below:
1) Start a new terminal session.
Navigate to the location where you extracted the pyODBC source files by typing cd ~/Downloads/pyODBC-3.0.6/ at the command line.
2a) Proceed to install pyODBC by typing python ./setup.py install on the command line.
If you received a screen like the below, it means that your user account do not have the necessary permissions to install pyODBC onto the system.
Type sudo python ./setup.py install on the command line to use a superuser account to perform the installation.
3) If the installation succeed, you should see a screen like the below:
Configure system environment variables
pyODBC has a dependency on the file libodbc.so.
This is the library of functions that pyODBC use to perform ODBC operations.
If pyODBC cannot find this file, you may see the following message:
“ImportError: <libodbc.so name> cannot open shared object file: No such file or directory”
The actual name <libodbc.so name> may varied from system to system. A sample screen is as follows:
To help pyODBC find the library, we need to declare an environment variable LD_LIBRARY_PATH and specify the location of the libodbc.so in this variable.
Assuming my libodbc.so is located at the directory /usr/local/lib
1) Type the following at the command line, to set the variable:
2) Type the following to set the variable as a global variable so that the executing environment can access this variable.
The changes made in the previous step are not persistent.
This means you need to carry out the same steps every time you start a new terminal session.
To make this change persistent, I recommend you put these 2 commands in your shell’s startup file.
If you are using the default bash shell, this would be your ~/.bashrc file.
Put the following 2 lines at the bottom of the file and save the file.
For testing pyODBC, I have the following python script:
database = pyodbc.connect('DRIVER=FreeTDS;SERVER=SARA\\SQLEXPRESS;UID=dbadmin;PWD=password;DATABASE=Arbalest;TDS_Version=8.0;',unicode_results=True)
cursor = database.cursor()
encoding = 'utf-8'
sql = "SELECT * FROM Arbalest.dbo.test;"
for row in cursor: print row
This python script assumes:
- I am trying to connect to the “SQLEXPRESS” Sql Server instance on a machine with the hostname of SARA
- The user account that I am using to log into the database is “dbadmin”
- The password to my user account is “password”
- The database that I am connecting to is “Arbalest”
For Sql Server later than 2005, having the parameter TDS Version=8.0 is recommended.
If this parameter is not defined in the connection string, the pyODBC may work correctly.
Assuming I save this script to a file call “test.py”, I would be able to run the script by typing the following on the command line:
Note that the connection string that I used above in the pyodbc.connect statement is what is referred to as DSN-less connection string.
With this you should be able to connect to Sql Server from python using pyODBC.