Home » ComputerScience » Adding the Oracle modules for Python on Linux

Adding the Oracle modules for Python on Linux

You’re going to need to get a few libraries here: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html.

RHEL 7
oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-basiclite-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-jdbc-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-odbc-12.2.0.1.0-1.x86_64.rpm

RHEL 6
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-basiclite-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-jdbc-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-odbc-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-tools-11.2.0.4.0-1.x86_64.rpm

Now you’ll need to add cx_Oracle:
https://oracle.github.io/python-cx_Oracle/
if you are having issues with pip, you can try this link : https://pypi.python.org/pypi/cx_Oracle/5.3

Now you can start coding Python to interface with Oracle.


#!/usr/local/Python-2.7.13/bin/python

import sys
import cx_Oracle

###############################
# Database connection info
# and connection string
###############################
host='hostname'
username='username'
password='password'
database='database_name'
port = 1521

try:
dsn = cx_Oracle.makedsn(host, port, database)
con = cx_Oracle.connect(username+'/'+password+'@' + dsn)
except ValueError:
print "error making database connection"
exit(1)

###############################
# run the SQL
###############################
try:
cur = con.cursor()
cur.execute("SELECT * from DB.FOO")
except ValueError:
print "error executing SQL"
exit(1)

###############################
# get the results
###############################
# the results come in an array .. so while I'm printing the whole "row" I can also access the results
# with syntax like row[0] or row[1]

for row in cur.fetchall():
print row

if I wanted to access the results at once instead of row by row I can also do this:

for result in cur:
print result

One Response so far.

  1. […] comes with built in support for MySQL. For Oracle, I needed to download the drivers and support (Adding the Oracle modules for Python on Linux) import […]

Leave a Reply