Connecting Python to Oracle.

In the current world of heterogeneous networks it seems that you need to be able to connect to ever type of database system out there. In a recent project, I was asked, to take an existing MySQL and Python system and get it to connect to an existing Oracle Data Warehouse system. To be honest, I hadn’t actually ever connected Python to an Oracle Database before and from experience this would be either very painful or pretty straight forward.

Of course like everything Python it was pretty straight forward. The first thing you will need to get is the correct cx_Oracle module from http://cx-oracle.sourceforge.net/. The cx_Oracle module allows you to connect to Oracle databases and it conforms to the Python database API specification. Which makes life easier for everyone. After you have installed the module the rest is just a matter having the right permission to connect to the Oracle Database and writing the code.

Here is quick script showing you how to connect to Oracle with Python.

#!/usr/bin/python

import cx_Oracle

connstr=’scott/tiger’
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()
curs.arraysize=50
curs.execute(‘select 2+2 “aaa” ,3*3 from dual’)
print curs.description
print curs.fetchone()
conn.close()

For more information check out the Python Programming Language – Official Website

Leave a Reply