Oracle – Connect DB with Python cx_Oracle

It is a Python extension module that enables access to Oracle Database.
You can use cx_Oracle with Oracle 11.2, 12.1 and 12.2 and 18.3 client libraries. Oracle’s standard client-server version interoperability allows connection to both older and newer databases. For example Oracle 18.3 client libraries can connect to Oracle Database 11.2.

  • The cx_Oracle project is open source and maintained by Oracle.
  • SQL and PL/SQL Execution.
  • Oracle data type support, including large object support (CLOB and BLOB).
  • Direct binding to SQL objects.

Syntax

>>> import cx_Oracle
>>> connection = cx_Oracle.connect('userid/password@IP:PORT/SID')

Example

# Python 3.7.2
>>> import cx_Oracle as cxo
>>> connection = cxo.connect('hr/open@localhost:1521/XE')
>>> cursor = connection.cursor()
>>> query = ("SELECT first_name, last_name FROM hr.employees")
>>> query_execute = cursor.execute(query)
>>> rows = query_execute.fetchall()
>>> for row in rows:
...     print(row[0], row[1])
...
Ellen Abel
Sundar Ande
Mozhe Atkinson
David Austin
Hermann Baer
Shelli Baida
Amit Banda
>>> cxo.version
'7.0.0'
>>> connection.dsn
'localhost:1521/XE'
>>> connection.version
'11.2.0.2.0'
>>> cursor.close()
>>> connection.close()

Reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s