Blog

Connect to an oracle database using python?

 

Step-1:download and install Python for 64-bit Windows

-go to URL: https://www.python.org/downloads/windows/

-scroll down the list until you see Python 2.7.1 – 2010-11-27

-download Windows x86-64 MSI installer (filename: python-2.7.1.amd64.msi)

-go through the installer by accepting all the defaults. The install directory will be C:\Python

-once the installation is complete, add the following locations to the windows PATH variable: C:\Python and C:\Python\Lib\site-packages

-open a command window and launch the python interpreter by running ‘python’ on the command-line prompt

STEP-2: open a command window and launch the python interpreter by running ‘python’ on the command-line prompt

C:\Users\AHMED>python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

 STEP 3:Install cx_Oracle

python -m pip install cx_Oracle --upgrade --user

STEP 4:Python Script to connect and Run SQL Query

C:\Users\AHMED>python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> conn = cx_Oracle.connect('username/abc123@xx.xxx.xxx.xx/BATCH47')
>>> ver = conn.version.split(".")
>>> print(ver)
['19', '3', '0', '0', '0']
>>> cursor = conn.cursor()
>>> query = "select count(*)from student"
>>> results = cursor.execute(query).fetchall()
>>> print(results)
[(6,)]
>>> conn.close()
>>>
>>>
>>> ^Z


C:\Users\AHMED>

Hope it Helps !

Follow me

Contact us for Training/ Job Support

Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.