问题
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:
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})
来源:https://stackoverflow.com/questions/51790471/binding-variables-to-sqlalchemy-query-for-pandas-read-sql