Python 索引器错误:元组索引超出psycopg2的范围
此代码没有问题:Python 索引器错误:元组索引超出psycopg2的范围,python,python-3.x,psycopg2,Python,Python 3.x,Psycopg2,此代码没有问题: cursor.execute("select message from snippets where keyword=%s", (name,)) 然而,我得到了一个索引器:元组索引超出范围 cursor.execute("select * from table where prescription like '\"%\"%s%'", (string,)) 我哪里出错了?第二个代码段将变量放在字符串文字中(因为它被单引号包围),所以psycopg不会处理它。解决此问题的一种方
cursor.execute("select message from snippets where keyword=%s", (name,))
然而,我得到了一个索引器:元组索引超出范围
cursor.execute("select * from table where prescription like '\"%\"%s%'", (string,))
我哪里出错了?第二个代码段将变量放在字符串文字中(因为它被单引号包围),所以psycopg不会处理它。解决此问题的一种方法是保留占位符表单,并在绑定之前在Python代码中执行字符串操作:
name = '%%%s%%' % name
cursor.execute("select * from table where prescription like %s", (name,))
要在查询中输入文字
%
,必须使用%