【问题标题】:IndexError: tuple index out of range with psycopg2IndexError:元组索引超出 psycopg2 的范围
【发布时间】:2017-08-23 22:14:54
【问题描述】:

这段代码没有问题:

cursor.execute("select message from snippets where keyword=%s", (name,))

但是,我收到了IndexError: tuple index out of range

cursor.execute("select * from table where prescription like '\"%\"%s%'", (string,))

我哪里出错了?

【问题讨论】:

    标签: python python-3.x psycopg2


    【解决方案1】:

    第二个 sn-p 将变量放在字符串文字中(因为它被单引号包围),所以 psycopg 不处理它。解决此问题的一种方法是保留占位符形式并在绑定之前在 Python 代码中执行字符串操作:

    name = '%%%s%%' % name
    cursor.execute("select * from table where prescription like %s", (name,))
    

    【讨论】:

      【解决方案2】:

      要在查询中输入文字 %,您必须使用 %%

      【讨论】:

        猜你喜欢
        • 2013-07-28
        • 2018-11-16
        • 2017-07-12
        • 2013-12-16
        • 2021-12-21
        • 2014-08-01
        • 2021-04-19
        • 1970-01-01
        相关资源
        最近更新 更多