To use Python with Oracle
below components must all be in place and be of the same version (Python 3.6
suggested) and same architecture (64-bit suggested).
Windows 7 x64
Oracle Database 12c
Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Python 3.6 x64
(python-3.6.0-amd64.exe)
cx_Oracle
(cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe)
Instance running in
64-bit. To do this, we must have installed:
- Part 1: Oracle Database 12c Enterprise Edition Release
12.1.0.2.0 - 64bit Production
Or Oracle 12c 64-bit
Windows Instant Client
- Part 2: Python 3.6 x64 (python-3.6.0-amd64.exe) (64bit)
- Part 3: cx_Oracle
(cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe) (64bit)
Part 1: Install Oracle
Database 12c
Download and install Oracle Database 12c from below location
or
Download and install Oracle Database 12c Instant Client from below
location
Part 2: Install Python
3.6 x64
Download and install Python 3.6 x64 (Windows
x86-64 executable installer) from
below location
Part 3: Install Python interface to Oracle (cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe)
Download and install Python interface to Oracle (cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe) from below location
Does it Work?
Now it is time to test
the connection. We can execute the following code to test the connectivity.
Steps
Step 1: Create db_conn_test.py
file
import cx_Oracle
con =
cx_Oracle.connect('hr/hr@orcl')
# do some stuff here
#ver =
con.version.split(".")
#print(ver)
print(con.version)
con.close()
Step 2: open IDLE (Python 3.6 64-bit)
File -> Open ->
select db_conn_test.py file
Step 3:
Run -> Run Module
Output:
Python 3.6.0
(v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on
win32
Type
"copyright", "credits" or "license()" for more
information.
>>>
============ RESTART:
D:\Study\Python\Python_code\db_conn_test.py ============
12.1.0.2.0
>>>
It will display current
oracle database version.
Example: Display data from HR
schema tables
import cx_Oracle
con =
cx_Oracle.connect('hr/hr@orcl')
# do some stuff here
ver =
con.version.split(".")
print(ver)
cur = con.cursor()
cur.execute('select *
from EMPLOYEES e where e.employee_id < 150 order by 2')
for result in cur:
print(result)
cur.close()
con.close()
I hope you all have enjoyed reading this article. Comments are welcome....
Related Posts:
- Oracle subprograms and Python Programming
Related Posts:
- Oracle subprograms and Python Programming
Hello-
ReplyDeleteI get the following error:
Traceback (most recent call last):
File "C:\Users\n882370\Desktop\Softwares\Python_Related\Test_1.py", line 1, in
import cx_Oracle
ImportError: DLL load failed: The specified module could not be found.
Could you please help?
Thank you for your time!
Sometimes conn string will look like below
ReplyDeleteconnection = cx_Oracle.connect('mittnag/ZAq12wSX$$@lonodsop01-sc.uk.db.com:1621/LNDBP1U.uk.db.com')