6

Im executing the following code, the purposes of the exeuction is to create a lookup-table in the Oracle data base to speed up my load of data. The table I want to load in is simply a vector with ID values, so only one column is loaded.

The code is written per below:

lookup = df.id_variable.drop_duplicates()

conn = my_oracle_connection()
obj = lookup.to_sql(name = 'lookup', con = conn, if_exists = 'replace')

I get the following error when exeucting this:

DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ORA-01036: illegal variable name/number

I can execute a psql.read_sql() query but above fails.

Now, I dont exactly know how to go about fixing it, im quite new to the technical aspects of getting this to work so any pointers in what direction to take it would be greately appriciated.

Thanks for any time and input!

5
  • Is conn an SQLAlchemy connection? Commented Mar 15, 2016 at 8:39
  • I did some checking in the underlyinc script which loades the connections and it seems to be an cx_Oracle.connect() setup. Commented Mar 15, 2016 at 8:50
  • DataFrame.to_sql() doesn't seem to support DBAPI2 connections to Oracle. Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported. That would seem to be supported by the error message related to sqlite_master too. Commented Mar 15, 2016 at 8:52
  • Ok, I think I follow, seems to be relatively straightforward. I will try and see if I can change the setup towards SQLAlchemy for my purpose. Could it be possible to load the look-up using the current engine, i.e. cx_Oracle? Maybe by some other function? Commented Mar 15, 2016 at 8:56
  • @JoachimIsaksson, same problem with to_sql using Oracle XE Commented Oct 16, 2019 at 6:29

2 Answers 2

6

I had the same issue when using cx_Oracle connection (I was able to use .read_sql function, but not the .to_sql one)

Use SQLalchemy connection instead:

import sqlalchemy as sa
oracle_db = sa.create_engine('oracle://username:password@database')
connection = oracle_db.connect()
dataframe.to_sql('table_name', connection, schema='schema_name', if_exists='append', index=False)
Sign up to request clarification or add additional context in comments.

1 Comment

Can you share the reason why cx_oracle does not work?
0

I think the problem happens writing to the Oracle DB using a connection object created by cx_Oracle. SqlAlchemy has a work around:

import cx_Oracle
from sqlalchemy import types, create_engine

conn = create_engine('oracle+cx_oracle://Jeremy:SuperSecret@databasehost:1521/?service_name=gdw')

df.to_sql('TEST', conn, if_exists='replace')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.