paint-brush
Saving Dataframes into Oracle Database with Pythonby@luca1iu
6,122 reads
6,122 reads

Saving Dataframes into Oracle Database with Python

by Luca LiuJanuary 15th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Here are two common errors that you'll want to watch out for when using the to_sql method to save a data frame into an Oracle database.
featured image - Saving Dataframes into Oracle Database with Python
Luca Liu HackerNoon profile picture

Using Python to Save a Dataframe into Oracle Database

You can use the pandas library in combination with the sqlalchemy and cx_Oracle libraries to save a dataframe to an Oracle database in Python.

import pandas as pd
from sqlalchemy import create_engine
import cx_Oracle

# Create an SQLAlchemy engine using the connection
engine = create_engine('oracle://username:password@hostname:port/service_name')

# Save the dataframe to the Oracle database and lowercase your_table_name 
df.to_sql('your_table_name', con=engine, if_exists='replace', index=False)

Replace 'username', 'password', 'hostname', 'port', and 'service_name' with your actual Oracle database credentials and connection details.


Please replace your_table_name with the actual lowercase name of the table in your Oracle database. Otherwise, you may encounter InvalidRequestError: Could not reflect: requested table(s) not available in Engine.


When you run this script, it will establish a connection to the Oracle database, create a sample dataframe, and then save the dataframe to the specified table in the Oracle database.

Make sure the table structure has been created in the database before proceeding.

Option 1: Create Table Structure using SQL

Keep in mind that you will need to create the table structure in the Oracle database before attempting to save the dataframe to it.


The table structure should match the structure of the dataframe in terms of column names and data types.


You can create the table structure in your Oracle database using SQL. Here's an example of how you might create a table in Oracle to match the structure of your dataframe:

CREATE TABLE your_table_name (
    column1_name  column1_data_type,
    column2_name  column2_data_type,
    ...
);

Option 2: Create Table Structure using Python

You can run the SQL script in Python using the engine.execute() method provided by the SQLAlchemy library. Here's an example of how you can execute an SQL script after creating the engine:

from sqlalchemy import create_engine

# Create an engine
engine = create_engine('oracle://username:password@hostname:port/database_name')

# SQL script to create a table
sql_script =
CREATE TABLE your_table_name (
    column1_name  column1_data_type,
    column2_name  column2_data_type,
    ...
);

# Execute the SQL script
with engine.connect() as connection:
    connection.execute(sql_script)

Make sure to replace your_table_name, column1_name, column1_data_type, etc., with your actual table and column names and data types.

Conclusion

Once the table structure is in place, you can then use the method I mentioned earlier to save the dataframe to the Oracle database. The to_sql method will automatically insert the data from the dataframe into the existing table in the database as long as the column names and data types match.


Thank you for taking the time to explore data-related insights with me. I appreciate your engagement. If you find this information helpful, I invite you to follow me or connect with me on LinkedIn. Happy exploring!👋