Friday, August 26, 2011

Connect Oracle in python

1, download cx_Oracle module

from http://cx-oracle.sourceforge.net/

[windows] double click the .msi file

[Ubuntu]

1,download .rpm file,

2, install alien tool in Ubuntu

3, install rpm by command “sudo alien -i cx_Oracle-5.1-10g-py27-1.i386.rpm ”

4, mv cx_Oracle related stuff (cx_Oracle-5.1-py2.7.egg-info & cx_Oracle.so) from site-packages/ to dist-packages/

5, add “export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle/xe/app/oracle/product/10.2.0/client/lib” in .bashrc

6, source .bashrc

 

2, install oracle client, express version is enough

http://www.oracle.com/technetwork/database/express-edition/index.html

[Windows]set system variable: “ORACLE_HOME” to the path of where express client installed, e.g. D:\XEClient, (this step help cx_Oracle locate some DLLs under Client/bin folder)

[Ubuntu] download and install oracle-xe-client_10.2.0.1-1.0_i386.deb

 

4, make sure you can import cx_Oracle in python correctly now.

python –c ‘import cx_Oracle’

 

5, start coding in python

conn = cx_Oracle.connect(‘username’, “password”, cx_Oracle.makedsn(‘10.1.1.20’, 1521, ‘servicename’)) #connect to db

cur = conn.cursor()

cur.execute(“select * from xxxtable”);

rows = cur.fetchall()

for r in rows:

print r

conn.close();

No comments:

Post a Comment