Python Oracle database connection using import cx_Oracle module


Please find the following scripting for selecting records from Oracle using python

You should download the cx_Oracle module first in order to use the following code

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

Please informed that both Python’s, cx module and Oracle client architectures have to be same for example if you are using Python 64 bit you should download 64 bit for cx and client or you can go on with 32 bit architecture.

#Created on Jul 25, 2013
#@author: tunatore

#python oracle connection example

import cx_Oracle

connection = cx_Oracle.connect("username/password@database")
cursor = connection.cursor()
cursor.execute("SELECT 'HELLO','WORLD' FROM USER_TABLES")
#row = cursor.fetchall()[0][0]
#direct row access
#row[0] referes to column one
for row in cursor:
 print (row[0], ",", row[1])

#executing DDL statement
cursor.execute("CREATE TABLE EMPLOYEE(ID NUMBER,EMPLOYEENAME VARCHAR2(20 BYTE))")
cursor.execute("INSERT INTO EMPLOYEE VALUES (:NUM,:NAME)",
 {
 'NUM' : 12345,
 'NAME' : 'TunaTore'
 }
 )

connection.commit()

#executing DDL statement
#cursor.execute("DROP TABLE EMPLOYEE")

cursor.close()
connection.close()



output

HELLO , WORLD
HELLO , WORLD
HELLO , WORLD
HELLO , WORLD
HELLO , WORLD
HELLO , WORLD

 

Advertisements

One thought on “Python Oracle database connection using import cx_Oracle module

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s