【问题标题】:sqlite3: passing function argument as part of SQL statementsqlite3:将函数参数作为 SQL 语句的一部分传递
【发布时间】:2021-08-25 03:35:35
【问题描述】:

使用 Python 中的 sqlite3 模块。有没有办法将函数参数传递给 SQL 语句?大致类似于下面的示例代码:

def func(param):
    
    cur.execute("""
    SELECT * FROM this_table
    WHERE this_column_value = param
    """)

【问题讨论】:

  • cur.execute(""" SELECT * FROM this_table WHERE this_column_value = (?) """, param)
  • @Epsi95 谢谢,您的代码在将 param 更改为 (param,) 后运行良好,但我不能接受评论作为答案
  • 您可以自己回答并接受以结束问题。

标签: python sql sqlite


【解决方案1】:
cur.execute("""
SELECT * FROM this_table
WHERE this_column_value = (?)    
""", (param,))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2020-10-28
    • 1970-01-01
    • 2023-03-24
    • 2013-06-03
    • 2018-06-12
    • 1970-01-01
    相关资源
    最近更新 更多