Python: Loading Oracle tables directly from Pandas (writing to Oracle)

Python: Loading Oracle tables directly from Pandas (writing to Oracle) … here is a solution to the problem.

Python: Loading Oracle tables directly from Pandas (writing to Oracle)

Is there any way to load data directly from Pandas into an Oracle table.

Currently I’m writing a dataset to a csv file and then loading the table. I want to bypass the “write csv” step.

I’m using cx_Oracle to connect to an Oracle database. URLs are passed as parameters when the Python script is called. The result is stored as a pandas dataframe in a variable dataset. The layout of the dataset and table definitions is the same.

    import cx_Oracle as cx
    response = requests.get(url)
    data = response.json()
    dataset = json_normalize(data['results'])

Please let me know if you need any further information.

Solution

Have you tried the to_sql function in the pandas module?

from sqlalchemy import create_engine
engine = create_engine('oracle://[user]:[pass]@[host]:[port]/[schema]', echo=False)
dataset.to_sql(name='target_table',con=engine ,if_exists = 'append', index=False)

Related Problems and Solutions