Python 3.x TypeError:在python与postgresql连接的字符串格式化过程中,并非所有参数都已转换
似乎代码中没有任何错误,但不知道我为什么会得到这个。 我正在创建一个简单的GUI应用程序,它将用户详细信息存储到数据库(postgresql)中,而且他们还可以在数据库中搜索条目。这个特定的错误集中在这个search()函数中,因此我没有添加其余的代码。如果有必要,我可以添加它们。 希望我能从这个社区得到一些解决方案Python 3.x TypeError:在python与postgresql连接的字符串格式化过程中,并非所有参数都已转换,python-3.x,database,string-formatting,postgresql-9.5,Python 3.x,Database,String Formatting,Postgresql 9.5,似乎代码中没有任何错误,但不知道我为什么会得到这个。 我正在创建一个简单的GUI应用程序,它将用户详细信息存储到数据库(postgresql)中,而且他们还可以在数据库中搜索条目。这个特定的错误集中在这个search()函数中,因此我没有添加其余的代码。如果有必要,我可以添加它们。 希望我能从这个社区得到一些解决方案 def search(id): conn = psycopg2.connect(dbname="postgres",user="postgres",password="10
def search(id):
conn = psycopg2.connect(dbname="postgres",user="postgres",password="1018",host="localhost",port="5432")
mycursor = conn.cursor()
query = '''select * from demotab where id=%s '''
mycursor.execute(query, (id))
row = mycursor.fetchone()
print(row)
conn.commit()
conn.close()
在下面获取此错误
Exception in Tkinter callback
Traceback (most recent call last):
File "c:\programdata\anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "appwithDB.py", line 51, in <lambda>
search_button = Button(newframe, text="Search", command=lambda : search(entry_search.get()))
File "appwithDB.py", line 71, in search
mycursor.execute(query, (id))
TypeError: not all arguments converted during string formatting
Tkinter回调中出现异常
回溯(最近一次呼叫最后一次):
文件“c:\programdata\anaconda3\lib\tkinter\\uuuuu init\uuuuu.py”,第1702行,在调用中__
返回self.func(*args)
文件“appwithDB.py”,第51行,在
search\u button=button(newframe,text=“search”,command=lambda:search(entry\u search.get())
搜索中的第71行文件“appwithDB.py”
执行(查询,(id))
TypeError:在字符串格式化过程中并非所有参数都已转换
mycursor.execute的第二个参数必须是包含要插入查询中的值的iterable 您可以使用一个列表:
mycursor.execute(查询,[id])
或者一个单元素元组:mycursor.execute(query,(id),)
注意逗号<代码>(id)与
id
相同。在python中,逗号是元组,而不是括号。非常感谢它的工作,我没想到会有这么快的响应。但是我想了解更多关于iterable的用法。为什么需要在那里使用iterable,为什么不只是一个变量?您的查询可能包含多个变量,因此必须使用iterable,它包含的元素数量与查询字符串中“%”的数量相同。这是图书馆的作者做出的选择。如果我的回答对您有帮助,请将其标记为“已接受”