4

Is it possible to bind variables to a SQLAlchemy query used in a Pandas.read_sql statement?

Using %s in the WHERE clause does not work and the documentation for cx_Oracle states:

cursor.execute('SELECT * FROM employees WHERE department_id=:dept_id)

Using the cx_Oracle driver directly is deprecated in Pandas and is not a viable option.

I have a list of groups I need to iterate through the WHERE statement as SELECT * is too large in memory to handle on a single PC.

EXAMPLE:

SELECT * FROM DUAL WHERE GROUP_NAME = %s

Returns this error:

(cx_Oracle.DatabaseError) ORA-00911: invalid character ... WHERE GROUP_NAME = %s

1 Answer 1

12

As you can see here, cx_Oracle.paramstyle is named not format. According to PEP 249 you have to use the :name syntax for named paramstyle:

import pandas as pd
sql = '''
 SELECT *
 FROM DUAL
 WHERE GROUP_NAME = :name
'''
df = pd.read_sql(sql, params={'name': the_name_you_want})

Sign up to request clarification or add additional context in comments.

1 Comment

what is the syntax for passing in a list of values for the parameter, instead of just one?

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.