如何传递 SELECT 查询的 IN 子句的参数来检索 pandas DataFrame?
尝试将参数传递给简单的 SELECT 查询:
query = """SELECT * from tbl_tab where name in {};"""
我通过这段代码来操作我的数据帧,其想法是能够将 1 到 n 参数传递到我的查询中:
conn = pyodbc.connect(conx_string)
t = tuple(["N","M"])
crsr = conn.cursor()
data = crsr.execute(query.format(t))
rows = [list(x) for x in data]
columns = [column[0] for column in crsr.description]
df = pd.DataFrame(rows, columns=columns)
我得到了预期的结果。但是,当将单个参数传递给 t 时:有 t = tuple(["N"])
我收到错误 编程错误:('42000',“[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]')'附近的语法不正确。(102) (SQLExecDirectW)")
有什么想法吗?
Trying to pass params to a simple SELECT query :
query = """SELECT * from tbl_tab where name in {};"""
I went through this code to manipulate my dataframe and the idea is to be able to pass 1 to n params into my query :
conn = pyodbc.connect(conx_string)
t = tuple(["N","M"])
crsr = conn.cursor()
data = crsr.execute(query.format(t))
rows = [list(x) for x in data]
columns = [column[0] for column in crsr.description]
df = pd.DataFrame(rows, columns=columns)
And I got the expected result. However, when passing a single param to t : having t = tuple(["N"])
I got the errorProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near ')'. (102) (SQLExecDirectW)")
Any idea please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用元组的字符串表示形式注入到SQL语句中。它适用于
tuple(["N","M"])
,因为你得到… IN ('N', 'M')
。但是,tuple(["N"])
会失败,因为结果是… IN ('N',)
并且尾随逗号不是有效的 SQL 语法。考虑一种避免 SQL 注入并使用参数化查询的方法。首先,如果您将 pandas 与 SQLite 以外的任何数据库一起使用,则应该使用 SQLAlchemy。当您将
select()
对象传递给 pandas.read_sql_query()
方法时,SQLAlchemy 将自动构建所需的 SQL 语句:You are using the string representation of a tuple to inject into your SQL statement. It works for
tuple(["N","M"])
because you get… IN ('N', 'M')
. However, it fails fortuple(["N"])
because the result is… IN ('N',)
and the trailing comma is not valid SQL syntax. Consider an approach that avoids SQL injection and uses a parameterized query.First, if you are using pandas with any database other than SQLite you should be using SQLAlchemy. SQLAlchemy will automatically build the required SQL statement when you pass the
select()
object to pandas.read_sql_query()
method: