Created
September 23, 2022 17:46
-
-
Save iKunalChhabra/edd5e876cac36ecc3b7be0fceb3112ac to your computer and use it in GitHub Desktop.
Connect to oracle database using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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