To enable Python to use the edb-odbc driver, a Python compliant interface is needed to the ODBC API. A commercial product providing this functionality can be found at http://www.egenix.com/files/python/mxODBC.html. When building mxODBC with only unixODBC installed, so changes to the build process are necessary. O nce egenix-mx-commercial-2.0.6.tar.gz is unpacked, "mxCOMMERCIAL.py" needs to be edited with the following changes:
Delete the line containing "mx.ODBC.iODBC". This should be on line 76 in version 2.0.7.
Delete the lines starting at line 113 in version 2.0.7
Extension('mx.ODBC.iODBC.mxODBC', ['mx/ODBC/iODBC/mxODBC.c', 'mx/ODBC/iODBC/mxSQLCodes.c' ], include_dirs=['mx/ODBC/iODBC', '/usr/local/iODBC/include'], define_macros=[('iODBC', None)], library_dirs=['/usr/local/iODBC/lib'], libraries=['iodbc'] ),
Once Python is properly configured, the following script connecting to the EnterpriseDB sample database can be used as a sample to further development.
import mx.ODBC.unixODBC print "****Running EDB Python Sample****" selectQuery="SELECT to_char(sysdate, 'yyyymmdd hh24:mi:ss'), empno, ename, job FROM emp WHERE empno > 7900" insertStmt="INSERT INTO emp(empno,ename, sal, deptno) VALUES(8000,'SHARK', 100, 20)" updateStmt="UPDATE emp SET job = 'DBA' where empno = 8000" storedProcStmt="EXEC select_emp(8000)" deleteStmt="DELETE FROM emp WHERE empno = 8000" dbc=mx.ODBC.unixODBC.connect("EnterpriseDB", "edb", "edb") print "Connected to Database Type: " + dbc.getinfo (17)[1] cur=dbc.cursor() print "********* Test 1 ****************" print "Executing the following query:" print " " + selectQuery cur.execute(selectQuery) print "Results:" for i in cur.fetchall(): print i print "********* Test 2 ****************" print "Executing the following query:" print " " + insertStmt cur.execute(insertStmt) print "********* Test 3 ****************" print "Executing the following query:" print " " + updateStmt cur.execute(updateStmt) print "********* Test 4 ****************" print "Executing the following query:" print " " + storedProcStmt cur.execute(storedProcStmt) print "Results:" for i in cur.fetchall(): print i print "********* Test 5 ****************" print "Executing the following query:" print " " + deleteStmt cur.execute(deleteStmt)