Sunday, 12 October 2014

How to connect to Oracle 12c database using Python.

Python is an indent based language. Does not contain any begin or end like if / end if, loop / end loop.
Beginning of scope is in hanging indent and the entire scope is indented.

Like
>>if(codition)
>>     staterment 
>>     staterment

Just look into the indent for the statement below if condition.

Now about oracle client library in python.
You can download oracle client cx_Oracle  from here : https://pypi.python.org/pypi/cx_Oracle/5.1.3.

cx_Oracle-5.1.3-11g.win-amd64-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 323KB
cx_Oracle-5.1.3-11g.win-amd64-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 322KB
cx_Oracle-5.1.3-11g.win-amd64-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 322KB
cx_Oracle-5.1.3-11g.win32-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 288KB
cx_Oracle-5.1.3-11g.win32-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 283KB
cx_Oracle-5.1.3-11g.win32-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 282KB
cx_Oracle-5.1.3-12c.win-amd64-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 323KB
cx_Oracle-5.1.3-12c.win-amd64-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 322KB
cx_Oracle-5.1.3-12c.win-amd64-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 322KB
cx_Oracle-5.1.3-12c.win32-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 288KB
cx_Oracle-5.1.3-12c.win32-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 283KB
cx_Oracle-5.1.3-12c.win32-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 282KB
cx_Oracle-5.1.3.tar.gz (md5) Source 2014-05-25 102KB

Just choose according to your version of python installed.

Install it in your machine if you are using Windows 32 bit download win32-py else if you are using Windows 64 bit  download win-amd64-py.

Here is the sample code below which will help you to connect to Oracle.

#---------------------------------------------------------------------------------------------------
import cx_Oracle
# con = cx_Oracle.connect('username/password@[ipaddress or hostname]/SID')
# for example con = cx_Oracle.connect('scott/tiger@127.0.0.1/orcl')
# Above 2 lies are format of connection string and sample connection string.
# Below is the actual connection string I have used. orcl.localdomain is the service name you have configured with net config assistant in oracle databsae server.

con = cx_Oracle.connect('hr/hr@192.168.0.109/orcl.localdomain')
cur = con.cursor()
cur.execute('select section_code,section_name from section_master order by 2')

for row in cur:

     print(row)
#closing all opened objects
file.close()
cur.close()
con.close()
print("File successfully exported")

#---------------------------------------------------------------------------------------------------

Please comment if you have any queries.
You can also find video tut0rialfor this lesson inhttps://www.youtube.com/watch?v=w3WVqn3WySs
Also you can subscribe to my youtube channel https://www.youtube.com/subhro190776

No comments: