How to use Actual ODBC Drivers with Python under Mac OS
Installation of ODBC package for python
There is free for noncommercial ODBC package for python from
www.egenix.com
Two packages have to be downloaded:
mxBase package
mxCommercial package
Both are source packages which must be compiled before use.
Installation of mxBase package
Open terminal
cd yourDownloadDir
tar zxvf egenix-mx-base-x.x.x.tar.gz
cd egenix-mx-base-x.x.x
python setup.py build
sudo python setup.py install
mxBase package should compile and install without problems.
Installation of mxCommercial package
Open terminal
cd yourDownloadDir
tar zxvf egenix-mx-commercial-x.x.x.tar.gz
cd egenix-mx-commercial-x.x.x
Now if you will try to compile the package you will not succeed:
python setup.py build
will produce an error message like this:
-----------------------------------------------------------
In file included from mx/ODBC/unixODBC/mxODBC.c:33:
mx/ODBC/unixODBC/mxODBC.h:387:24: sqlucode.h: No such file or directory
error: command 'gcc' failed with exit status 1
-----------------------------------------------------------
mxODBC is designed to use both iODBC and unixODBC connectors. Mac OS does not come with unixODBC. To be able to compile egenix ODBC support you have to open mxCOMMERCIAL.py from the egenix-mx-commercial package in your favorite text editor and comment out all lines about the unixODBC driver.
After that, the build should work:
python setup.py build
sudo python setup.py install
Testing ODBC driver with python
Open terminal and type python in the command lane.
When the python interactive
interface appears, type the following:
>>> from mx.ODBC import iODBC
>>> connection=iODBC.DriverConnect('DSN=yourDtasource;UID=yourUsername;PWD=yourPassword')
>>> cursor=connection.cursor
or make a simple test script like this:
odbctest.py
----------------------------------------
#!/usr/env python
from mx.ODBC import iODBC
from mx.ODBC.iODBC import SQL
connection=iODBC.DriverConnect('DSN=yourDatasource;UID=yourUsername;PWD=yourPassword')
connection.setconnectoption(SQL.AUTOCOMMIT,SQL.AUTOCOMMIT_ON)
cursor=connection.cursor()
cursor.execute('select afield from atable')
print cursor.fetchall()
----------------------------------------