Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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如何处理TypeError:在字符串格式化过程中并非所有参数都已转换_Python_Postgresql_Psycopg2 - Fatal编程技术网

Python psycopg2如何处理TypeError:在字符串格式化过程中并非所有参数都已转换

Python psycopg2如何处理TypeError:在字符串格式化过程中并非所有参数都已转换,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,我有一个由psycopg2实现的简单数据库查询 但我不知道为什么它总是显示错误 这是密码 ip ="127.0.0.1" sql="select count(*) from radacct where nasipaddress=%s" cur.execute(sql,ip) 然后它就会显现出来 TypeError:在字符串格式化过程中并非所有参数都已转换 如果我这样做的话 cur.execute("select count(*) from radacct where nasipaddres

我有一个由psycopg2实现的简单数据库查询 但我不知道为什么它总是显示错误 这是密码

ip ="127.0.0.1"
 sql="select count(*) from radacct where nasipaddress=%s"
 cur.execute(sql,ip)
然后它就会显现出来

TypeError:在字符串格式化过程中并非所有参数都已转换

如果我这样做的话

cur.execute("select count(*) from radacct where nasipaddress=%s" % ip)
它仍然不起作用


如何以正确的方式将参数传递给psycopg2。请帮助我

传递给
execute
的sql参数必须在元组或列表中,即使只有一个元组或列表。这在以下文件中有所说明:

对于位置变量绑定,第二个参数必须始终为 序列,即使它包含单个变量。记住这一点 Python需要逗号来创建单个元素元组:

所以你需要这样做:

ip ="127.0.0.1" 
sql="select count(*) from radacct where nasipaddress=%s"
cur.execute(sql, (ip,))
可能重复的