2024-05-31 23:58:59 发布
网友
此代码没有问题:
cursor.execute("select message from snippets where keyword=%s", (name,))
但是,我得到了一个IndexError: tuple index out of range
IndexError: tuple index out of range
我哪里出错了?在
为了在查询中输入文本%,必须使用%%。在
%
%%
第二个代码段将变量放入字符串文本(因为它被单引号括起来),所以psycopg不处理它。解决此问题的一种方法是保留占位符表单,并在绑定之前在Python代码中执行字符串操作:
name = '%%%s%%' % name cursor.execute("select * from table where prescription like %s", (name,))
为了在查询中输入文本
%
,必须使用%%
。在第二个代码段将变量放入字符串文本(因为它被单引号括起来),所以psycopg不处理它。解决此问题的一种方法是保留占位符表单,并在绑定之前在Python代码中执行字符串操作:
相关问题 更多 >
编程相关推荐