【问题标题】: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])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2013-10-27
      • 1970-01-01
      • 2022-10-15
      • 2011-08-07
      相关资源
      最近更新 更多