In this tutorial, we will cover how to connect Python with Oracle database and execute SQL queries. We will be using HTML tags to structure our tutorial for better readability.
Python & Oracle – Conexión y SQL
Step 1: Install Required Libraries
First, you need to install the required libraries for connecting Python with Oracle. You can use the following pip command to install them:
pip install cx_Oracle
Step 2: Establish Connection with Oracle Database
Next, you need to establish a connection with your Oracle database using the following Python code:
import cx_Oracle
# Connect to Oracle database
connection = cx_Oracle.connect('username/password@hostname/service_name')
Step 3: Create a Cursor Object
After establishing a connection, you need to create a cursor object to execute SQL queries. Here’s how you can do it:
# Create a cursor object
cursor = connection.cursor()
Step 4: Execute SQL Queries
Now, you can execute SQL queries using the cursor object. Here’s an example of executing a SELECT query:
# Execute a SELECT query
cursor.execute('SELECT * FROM table_name')
# Fetch and print results
for row in cursor.fetchall():
print(row)
Step 5: Close Cursor and Connection
Finally, don’t forget to close the cursor and connection to free up resources. Here’s how you can do it:
# Close cursor
cursor.close()
# Close connection
connection.close()
Complete Python Script
Here’s the complete Python script to connect to Oracle database and execute SQL queries:
import cx_Oracle
# Connect to Oracle database
connection = cx_Oracle.connect('username/password@hostname/service_name')
# Create a cursor object
cursor = connection.cursor()
# Execute a SELECT query
cursor.execute('SELECT * FROM table_name')
# Fetch and print results
for row in cursor.fetchall():
print(row)
# Close cursor
cursor.close()
# Close connection
connection.close()
That’s it! You have successfully connected Python with Oracle database and executed SQL queries. Feel free to modify the code as needed for your specific use case.
I hope this tutorial was helpful. If you have any questions, feel free to ask.
Thanks for sharing. I would have liked to have seen how to use bind variables instead of the hardcoded WHERE clause values. Using bind variables is important for security & scalability. They are not difficult to use. Teaching how to use them is important so that all newcomers start with best-practice code.
Excelente tutorial y muy bien explicado.
Un solo comentario que dejo por las dudas que a otro le pase y es que en la conexión, además de la URL, tuve que poner dos puntos y el puerto, por lo que quedó algo asÅ
cnx = oracledb.connect(
user = 'USUARIO',
password= 'PASSWORD',
dsn = 'URL:PUERTO/NOMBRE_BD'
)
SerÃa bueno si pudieras añadir un enlace al vÃdeo desde donde se puede ver la descarga de Oracle.
Graças amigo , hablo de Brasil y unsted mi ayudo mucho ! Muchas gracias y saludos !
Hola, es la primera vez que escribo un comentario, pero muchas gracias por el video, tenÃa el problema de no poder crear usuarios y hacer la conexión, pero descargando una nueva imagen siguiendo tus videos solucioné todo, ahora solo me falta programar el resto de mi interfaz por mi cuenta, gracias!!!
Super buen tutorial, muy didactico de tu parte!