TypeError:并非在与postgresql连接的python字符串格式化期间转换的所有参数

问题描述 投票:0回答:1

似乎代码中都没有错误,但是不知道为什么我得到了这个。我正在创建一个简单的GUI应用程序,该应用程序将用户详细信息存储到数据库(PostgreSQL)中,并且他们将能够在数据库中搜索条目。此特定错误发生在此search()函数中,因此我没有添加其余代码。如有必要,我可以添加它们。希望我能从这个社区得到一些解决方案。

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
python-3.x database string-formatting postgresql-9.5
1个回答
0
投票
您可以使用列表:mycursor.execute(query, [id])或单元素元组:mycursor.execute(query, (id,))

请注意逗号。 (id)id相同。在python中,逗号是元组,而不是括号。

© www.soinside.com 2019 - 2024. All rights reserved.