0% found this document useful (0 votes)
4 views5 pages

Connect Oracle DB With Python

This document provides a step-by-step guide on how to connect an Oracle database with Python using the cx_Oracle library. It includes instructions for installing necessary software, setting up the Oracle Instant Client, and establishing a database connection with example code to fetch and display data from a table. The guide emphasizes the importance of configuring the connection parameters correctly for successful database interaction.

Uploaded by

zeb_usman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Connect Oracle DB With Python

This document provides a step-by-step guide on how to connect an Oracle database with Python using the cx_Oracle library. It includes instructions for installing necessary software, setting up the Oracle Instant Client, and establishing a database connection with example code to fetch and display data from a table. The guide emphasizes the importance of configuring the connection parameters correctly for successful database interaction.

Uploaded by

zeb_usman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CONNECT ORACLE DATABASE WITH PYTHON

Akbar Ali Sayed


1. Install python latest version.

https://www.python.org/downloads/ OR Microsoft store

2. Install Microsoft Visual C++ Build Tools:


https://visualstudio.microsoft.com/visual-cpp-build-tools/

Choose Desktop Development with C++

LinkedIn-: Akbar Ali Sayed


3. Install cx_Oracle.

pip install cx_Oracle

4. Install Oracle instant Client softwere.

I already installed you got softwere from oracle support it is light weight bundle.

-Add entries of database into instant client tnsnames.ora

5. Import cx_Oracle and connection of db

 import cx_Oracle

# Provide the correct path with escaped backslashes


lib_dir_path = r"D:\\chrome download\\instantclient-basic-windows.x64-
21.12.0.0.0dbru\\instantclient_21_12"

# Initialize the Oracle client


LinkedIn-: Akbar Ali Sayed
cx_Oracle.init_oracle_client(lib_dir=lib_dir_path)

Here, we are importing the cx_Oracle module and initializing the Oracle client by providing the path to
the Oracle Instant Client directory. The r prefix before the path indicates a raw string, which is useful
for handling backslashes in Windows paths without escaping them.

 username = "PROD"
password = "prod"
host = "DESKTOP-7G8C61I"
port = "1521"
service_name = "orcl"

dsn_tns = cx_Oracle.makedsn(host, port, service_name=service_name)

We're setting the Oracle database connection parameters such as the username, password, host, port,
and service name. Then, you're creating a Data Source Name (DSN) using the makedsn function from
cx_Oracle.

 connection = cx_Oracle.connect(username, password, dsn=dsn_tns)

This line establishes a connection to the Oracle database using the provided connection parameters.

 cursor = connection.cursor()
We're creating a cursor object. Cursors are used to execute SQL statements and fetch results.

 # Execute the query to fetch all rows from the table


cursor.execute("SELECT * from employee")

# Fetch all rows


rows = cursor.fetchall()

# Display the content of the table


for row in rows:
print(row)

We're executing a simple SQL query to select all rows from the "employee" table, fetching the results,
and then printing each row.

 cursor.close()
 connection.close()
Finally, you're closing the cursor and the database connection to release resources.

LinkedIn-: Akbar Ali Sayed


Sample Output From My Server

Script for connection

import cx_Oracle

# Provide the correct path with escaped backslashes


lib_dir_path = r"D:\\chrome download\\instantclient-basic-windows.x64-21.12.0.0.0dbru\\instantclient_21_12"

# Initialize the Oracle client


cx_Oracle.init_oracle_client(lib_dir=lib_dir_path)

username = "PROD"
password = "prod"
host = "DESKTOP-7G8C61I"
port = "1521"
service_name = "orcl"

dsn_tns = cx_Oracle.makedsn(host, port, service_name=service_name)


connection = cx_Oracle.connect(username, password, dsn=dsn_tns)

# Now you can use the 'connection' object to interact with the Oracle database

cursor = connection.cursor()

# Execute the query to fetch all rows from the table


cursor.execute("SELECT * from employee")

# Fetch all rows


rows = cursor.fetchall()

# Display the content of the table


for row in rows:
print(row)

cursor.close()
connection.close()

LinkedIn-: Akbar Ali Sayed

You might also like