Python
From Oracle FAQ
Python is an interpreted, interactive, object-oriented programming language. It is often compared to Perl, Java and Tcl.
Contents |
[edit] Oracle connectivity
Before you can access a database, you need to install one of the many available database modules. One such module is cx_Oracle.
cx_Oracle is a Python extension module that allows access to Oracle databases and conforms to the Python database API specification. The cx_Oracle module must be imported as it's not part of the core Python language. Example:
python
>>> import cx_Oracle
>>> connection = cx_Oracle.connect('scott/tiger@orcl')
>>> # do some stuff here
>>> connection.close()
[edit] Fetching rows
Define a cursor to fetch rows. Examples:
connection = cx_Oracle.connect("uid/pwd@database")
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM User_Tables")
count = cursor.fetchall()[0][0]
cursor.close()
connection.close()
More complex example, using bind variables:
connection = cx_Oracle.connect("uid/pwd@database")
cursor = connection.cursor()
cursor.arraysize = 50
cursor.execute("""
select Col1, Col2, Col3
from SomeTable
where Col4 = :arg_1
and Col5 between :arg_2 and :arg_3""",
arg_1 = "VALUE",
arg_2 = 5,
arg_3 = 15)
for column_1, column_2, column_3 in cursor.fetchall():
print "Values from DB:", column_1, column_2, column_3
cursor.close()
connection.close()
[edit] Executing DDL and DML statements
Example to execute DML and DDL statements:
connection = cx_Oracle.connect("uid/pwd@database")
cursor = connection.cursor()
cursor.execute("CREATE TABLE x(a DATE)")
cursor.close()

