I need to connect Oracle DB and run scripts in Python but not sure how to do it. I currently use Oracle SQL Developer to run my queries. Please help, Attached screen shot has connection details from Oracle SQL Developer.
Asked
Active
Viewed 1.0k times
3

Santhosh
- 549
- 2
- 6
- 10
1 Answers
4
In the oracle web site there is a good tutorial that helped me a lot: Tutorial: Using Python with Oracle Database 11g.
I had to import the cx_Oracle library and I write the follow code:
import cx_Oracle
connection = cx_Oracle.connect('username/password@IP:Port/DatabaseName')
cur = connection.cursor()
cur.execute('my query')
for result in cur:
print(result)
cur.close()
connection.close()

Jesus Peralta
- 661
- 1
- 8
- 18
-
The old tutorial reference has been obsoleted. The current tutorial for the (now renamed cx_Oracle) driver is at https://oracle.github.io/python-oracledb/samples/tutorial/Python-and-Oracle-Database-The-New-Wave-of-Scripting.html – Christopher Jones Jul 05 '22 at 22:48