【问题标题】:Passing wildcard LIKE parameter to read_sql_query()将通配符 LIKE 参数传递给 read_sql_query()
【发布时间】:2022-11-20 02:37:50
【问题描述】:
每次运行下面的代码时,我都会收到执行失败的 sql 错误。
lookup = f'12545%'
sql = pd.read_sql_query(
'''
Select *
From table
Where Name like ?
'''
,conn,lookup)
基本上,我想我需要在双引号内传递以下参数作为参数:“'12545%'”
不确定执行此操作的最佳方法是什么。
我试过转义 ' 和 % 但仍然出现相同的错误,或者它说 12545 都不存在。
【问题讨论】:
标签:
python
sql
pandas
pyodbc
【解决方案1】:
您需要将参数作为关键字参数传递,因为它是函数的第 5 个位置参数。
您必须将所有参数放在列表或元组中,而不是单个字符串。
ql = pd.read_sql_query(
'''
Select *
From table
Where Name like ?
'''
,conn,params=[lookup])