Binding variables to SQLAlchemy query for Pandas.read_sql

只谈情不闲聊 提交于 2020-06-01 05:57:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!