Monday, February 7, 2011

To install cx_Oracle in Fedora for accessing Oracle form Python

To install cx_Oracle in Fedora8 (or in most of Linux flavor)
1) Download cx_Oracle form http://cx-oracle.sourceforge.net (latest version is 5.0.4)
(look for proper version).
Register and create an oracle account (It’s free and takes couple of minute). Accept license agreement and download.
Extract the files in /usr/lib/ you need to become root for that (use sudo).

3) Set the environment variables.(open bash_profile and set the path variables).
Open [xyz~] $ vi .bash_profile (I set the following path for my Fedora).

export ORACLE_HOME=/appl/paths/instantclient_10_2 <--note client version export LD_LIBRARY_PATH=$ORACLE_HOME/lib export PATH=$ORACLE_HOME/bin:$PATH

4) Install cx_Oracle by double click on cx_Oracle-5.0.4-10g-py25-1.i386.rpm or by running 'python setup.py install'.

5) To check if the installation works properly. $ python >>>import cx_Oracle
>>>

If no error message comes then you are ready to use oracle module.

Simple query to check your connection
#!/usr/bin/python
import cx_Oracle
connstr='scott/tiger'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()
curs.execute('select * from emp')
print curs.description
for row in curs:
print row
conn.close()

you can bypass TNS by using this as a connection string: scott/tiger@myhost:1521/DATABASE_SID
let me know if anyone is having any trouble configuring cx_Oracle.

1 comment:

nachouve said...

Thank you for the post.

I have had a problem using:

conn = cx_Oracle.connect('dbname/mypass@myhost:1521/MYSID')


But solved using function makedsn():

conn = cx_Oracle.connect('dbname/mypass@'+cx_Oracle.makedsn('myhost',1521, 'MYSID'))