Skip to content

Instantly share code, notes, and snippets.

@iKunalChhabra
Created September 23, 2022 17:46
Show Gist options
  • Save iKunalChhabra/edd5e876cac36ecc3b7be0fceb3112ac to your computer and use it in GitHub Desktop.
Save iKunalChhabra/edd5e876cac36ecc3b7be0fceb3112ac to your computer and use it in GitHub Desktop.
Connect to oracle database using python
import cx_Oracle
# Connect to the database
connection = cx_Oracle.connect("user", "password", "host:port/service_name")
# Create a cursor
cursor = connection.cursor()
try:
# Execute a statement
cursor.execute("select * from table")
# get column names
col_names = [row[0] for row in cursor.description]
# Fetch all the rows
rows = cursor.fetchall()
# Print the rows
for row in rows:
print(dict(zip(col_names, row)))
except Exception as e:
raise e
finally:
# Close the cursor
cursor.close()
# Close the connection
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment