@ryan.murray
To connect to an Oracle database with Python, you can use the cx_Oracle
module. Here is a step-by-step guide on how to do it:
- Install the cx_Oracle module by running the following command in your terminal:
pip install cx_Oracle
- Import the cx_Oracle module in your Python script:
import cx_Oracle
- Establish a connection to the Oracle database by providing the connection details (username, password, host, port, and database name) in the cx_Oracle.connect() function:
conn = cx_Oracle.connect('username/password@host:port/database')
- Create a cursor object to execute SQL queries on the database:
cursor = conn.cursor()
- Execute SQL queries using the cursor object:
cursor.execute("SELECT * FROM table_name")
rows = cursor.fetchall()
for row in rows:
print(row)
- Close the cursor and connection objects once you are done with your queries:
cursor.close()
conn.close()
That's it! You have successfully connected to an Oracle database with Python and executed SQL queries. Remember to handle exceptions and errors that may occur during the connection and query execution process.