DAY 13 - Connect to Oracle DB Using Python Script
DAY 13 - Connect to Oracle DB Using Python Script
Step 1 : Install Libraries# Pandas & cx_Oracle in Command Prompt
Refer this blog to install libraries in your system.
https://dsbyrr.blogspot.com/2019/10/steps-to-prepare-your-windows-laptop.html
Step 2 : Execute the below script in Python IDLE by passing Oracle DB Connection Details
import cx_Oracle
import pandas as pd
connection = None
try:
#Oracle DB Connection Details
username = 'p_user_name'
password = 'p_password'
dsn = 'hostname:port_number/service_name'
connection = cx_Oracle.connect(
username,
password,
dsn)
# show the version of the Oracle Database
print(connection.version)
cur = connection.cursor()
cur.execute("select * from oe_order_headers_all where rownum <=10") #fetching 10 records from Order Header Table
res = cur.fetchall()
#store data into a dataframe
df = pd.DataFrame(res)
df
#Command to dump data into a CSV file with an index column
df.to_csv("datadump.csv",index=True,header=True)
#logic to print each record
for row in res:
#pass
print(row)
cur.close()
except cx_Oracle.Error as error:
print(error)
finally:
# release the connection
if connection:
connection.close()
#RR #Day13 #Python #ConnectionToOracleDB #HappyLearning #WeLearnEveryday
Step 1 : Install Libraries# Pandas & cx_Oracle in Command Prompt
Refer this blog to install libraries in your system.
https://dsbyrr.blogspot.com/2019/10/steps-to-prepare-your-windows-laptop.html
Step 2 : Execute the below script in Python IDLE by passing Oracle DB Connection Details
import cx_Oracle
import pandas as pd
connection = None
try:
#Oracle DB Connection Details
username = 'p_user_name'
password = 'p_password'
dsn = 'hostname:port_number/service_name'
connection = cx_Oracle.connect(
username,
password,
dsn)
# show the version of the Oracle Database
print(connection.version)
cur = connection.cursor()
cur.execute("select * from oe_order_headers_all where rownum <=10") #fetching 10 records from Order Header Table
res = cur.fetchall()
#store data into a dataframe
df = pd.DataFrame(res)
df
#Command to dump data into a CSV file with an index column
df.to_csv("datadump.csv",index=True,header=True)
#logic to print each record
for row in res:
#pass
print(row)
cur.close()
except cx_Oracle.Error as error:
print(error)
finally:
# release the connection
if connection:
connection.close()
#RR #Day13 #Python #ConnectionToOracleDB #HappyLearning #WeLearnEveryday
Comments
Post a Comment