索引器错误:元组索引超出psycopg2的范围

2024-05-31 23:58:59 发布

您现在位置:Python中文网/ 问答频道 /正文

此代码没有问题:

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

但是,我得到了一个IndexError: tuple index out of range

^{pr2}$

我哪里出错了?在


Tags: of代码namefrommessageexecuteindexout
2条回答

为了在查询中输入文本%,必须使用%%。在

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

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

相关问题 更多 >