For communicating any database with our Python program, then we required some connector which is nothing but the cx_Oracle module.
pip install cx_Oracle
Now Establish a connection between Python program and Oracle database by using connect() function.
con = cx_Oracle.connect('username/password@localhost')
To execute sql query and to provide result some special object required is nothing but cursor() object
cursor = cx_Oracle.cursor()
cursor.execute(sqlquery) -- -- -> to execute single query.
cursor.execute(sqlqueries) -- -- -> to execute a group of multiple sqlquery seperated by ";"
For DML(Data Manuplate Language) operation we need to commit() to reflect in database.
This retrieves the next row of a query result set and returns a single sequence, or None if no more rows are available.
After all done mendentory to close all operation
cx_Oracletry: dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) c = conn.cursor() c.execute('select * from database.table') for row in c: print (row, '-', row) except cx_Oracle.
DatabaseErroras e: print("There is a problem with Oracle", e) #conn.close() finally: if cursor: cursor.close() if con: con.close()
Note -if needed, place an 'r' before any parameter in order to address any special character such as '\'.
If you like dEexams.com and would like to contribute, you can also write your article here or mail your article to email@example.com . See your article appearing on the dEexams.com main page and help others to learn.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.