Monday 12 November 2018

How to connect Oracle database using Pything 3.7

To connect oracle database from python ,we need cx_Oracle module to be installed.
let us first go thru  how to install cx_Oracle module and followed by writing a python script using this module
 

Step 1. Download & Install cx_Oracle ( Detail installation steps are in https://oracle.github.io/python-cx_Oracle/)
 
 
Installation methods.
 
There are various methods to install python modules. the following are most popular methods
  •      pip
  •      git
  •      Using source from pypi
I used the last option. let us quickly go thru all the steps

1.1 PIP:

python -m pip install cx_Oracle --upgrade

incase if proxy for the internet is not set you want to use the following
python -m pip install --upgrade --user --proxy http://proxy.xxxx.com:8080 cx_oracle

1.2 GIT:

git clone https://github.com/oracle/python-cx_Oracle.git cx_Oracle
cd cx_Oracle
git submodule init
git submodule update
python setup.py install

1.3 Install Using Source from PyPI ( https://pypi.org/project/cx_Oracle/#files)
The source package can be downloaded manually from PyPI and extracted, after which the following commands should be run from the target python version

python setup.py build
python setup.py install


I downloaded cx_Oracle version 7 on 3.7 python
 
 
Note:
cx_Oracle 7 has been tested with Python version 2.7, and with versions 3.5 through 3.7. You can use cx_Oracle with Oracle 11.2, 12.1 and 12.2 and 18.3 client libraries. Oracle's standard client-server version interoperability allows connection to both older and newer databases. For example Oracle 18.3 client libraries can connect to Oracle Database 11.2.
 
 

2 . Database connection script

cat db1.py

import sys
import cx_Oracle
username ='user1'
password='password'
databaseName='connect_string'
con = cx_Oracle.connect( username,password,databaseName)
cursor=con.cursor()
cursor.execute('select startup_time,sysdate from v$instance')
result=cursor.fetchall ()
for row in result:
   print ( row[0] )
   print ( row[1] )


Execution:

./python db1.py
2018-11-12 02:06:38
2018-11-13 02:54:36
 
 
 


No comments:

Post a Comment

ZFS

Public Cloud tools comparison