Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器错误:元组索引超出psycopg2的范围_Python_Python 3.x_Psycopg2 - Fatal编程技术网

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,))

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