Nearby lessons
105 of 108Python - PIP
Detailed Tutorial Notes
fetch
close()
These methods won't be changed from database to database and same for all databases.
Working with Oracle Database:
From Python Program if we want to communicate with any database,some translator must be
required to translate Python calls into Database specific calls and Database specific calls into
Python calls.This translator is nothing but Driver/Connector.
Diagram
For Oracle database the name of driver needed is cx_Oracle.
cx_Oracle is a Python extension module that enables access to Oracle Database.It can be used for
both Python2 and Python3. It can work with any version of Oracle database like 9,10,11 and 12.
Installing cx_Oracle:
From Normal Command Prompt (But not from Python console)execute the following command
D:\python_classes>pip install cx_Oracle
Collecting cx_Oracle
Downloading cx_Oracle-6.0.2-cp36-cp36m-win32.whl (100kB)
100% |-----------| 102kB 256kB/s
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-6.0.2
How to Test Installation:
From python console execute the following command:
main.py
....
In the output we can see cx_Oracle
_multiprocessing crypt ntpath timeit
_opcode csv nturl2path tkinter
_operator csvr numbers token
_osx_support csvw opcode tokenize
_overlapped ctypes operator trace
_pickle curses optparse traceback
_pydecimal custexcept os tracemalloc
_pyio cx_Oracle parser try
_random data pathlib tty
_sha1 datetime pdb turtle
_sha256 dbm pick turtledemo
_sha3 decimal pickle types
_sha512 demo pickletools typing
_signal difflib pip unicodedata
_sitebuiltins dis pipes unittest
_socket distutils pkg_resources unpick
_sqlite3 doctest pkgutil update
_sre dummy_threading platform urllib
_ssl durgamath plistlib uu
_stat easy_install polymorph uuid
.....App1: Program to connect with Oracle database and print its version.
main.py
import cx_Oracle
try:
con=cx_Oracle.connect('scott/tiger@localhost')
cursor=con.cursor()
cursor.execute("create table employees(eno number,ename varchar2(10),esal number(10,2),eaddr varchar2(10))")
print("Table created successfully")
except cx_Oracle.DatabaseError as e:
if con:
con.rollback()
print("There is a problem with sql",e)
finally:
if cursor:
cursor.close()
if con:
con.close()D:\python_classes>py db1.py
11.2.0.2.0
App2: Write a Program to create employees table in the oracle database :
employees(eno,ename,esal,eaddr)
App3: Write a program to drop employees table from oracle database?
1) import cx_Oracle
2) try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5) cursor.execute("drop table employees")
6) print("Table dropped successfully")
7) except cx_Oracle.DatabaseError as e:
8) if con:
9) con.rollback()
10) print("There is a problem with sql",e)
11) finally:
12) if cursor:
13) cursor.close()
14) if con:
15) con.close()App3: Write a program to insert a single row in the employees table.