【问题标题】:pysqlite not accepting qmark parameterizationpysqlite 不接受 qmark 参数化
【发布时间】:2014-03-07 19:50:16
【问题描述】:

我有一个与已回答的问题类似的问题 Python pysqlite not accepting my qmark parameterization

我的问题如下: 我想要一个类似的字符串的参数化搜索,而不是字符串本身。

这是我的声明:

command = "select id, l from testDT where l like '%, ?]'"
cur.command(command, (123,))

pysqlite 返回以下错误:

pysqlite2.dbapi2.ProgrammingError: Incorrect number of bindings supplied. The current     statement uses 0, and there are 1 supplied.

我明白这是因为 qmark 被解释为文字。但是,我不知道如何在不将 qmark 解释为文字的情况下使用 qmark 指定这样的“like”搜索。

以下搜索成功:

command = "select id, l from testDT where l like '%, {x}]' "
command = command.format(x=123)
cur.execute(command)

但是,据我了解,这正是人们应该使用 format() 函数的方式。

【问题讨论】:

    标签: python pysqlite parameterization


    【解决方案1】:

    您使用 whole lot 作为参数,例如:

    command = "select id, l from testDT where l like ? "
    cur.command(command, ('%, 123]',))
    

    【讨论】:

    • 谢谢!像魅力一样工作!
    • 也许还有一个问题:以这种方式使用该方法是否安全? x = 123 command = "select id, l from testDT where l like ?" cur.command(command, ('%, {}]'.format(x),))
    • @user142295 是的...只要您不将格式应用于 SQL 查询本身,您就可以应用格式来构建参数,并且该参数将为实际查询正确转义。
    猜你喜欢
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2012-09-04
    • 2020-03-31
    • 2016-10-31
    • 2018-03-16
    • 2023-04-03
    相关资源
    最近更新 更多