首页 > 解决方案 > Tkinter 回调中的异常 - TypeError:字符串格式化期间并非所有参数都转换

问题描述

在通过 id (int) 从 postgresql 表中获取记录时,我遇到了这个错误。如果你能帮我解决这个问题,我将不胜感激。


错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
    return self.func(*args)
  File "C:\Users\hp\Desktop\VENV Project\studentdataentryapp.py", line 71, in <lambda>
    button = Button(frame,text="Search By ID", command=lambda:search(id_search.get()))
  File "C:\Users\hp\Desktop\VENV Project\studentdataentryapp.py", line 27, in search
    cur.execute(query,(id))
TypeError: not all arguments converted during string formatting

这是代码

编码

def search(id):
    conn = psycopg2.connect(dbname="postgres", user="postgres", password="maryam")
    cur = conn.cursor()
    query = '''select * from student where id=%s;'''
    cur.execute(query,(id))
    row = cur.fetchone()
    print(row)
    conn.commit()
    conn.close()

id_search = Entry(frame)
id_search.grid(row=13, column=2, sticky=W)

button = Button(frame,text="Search By ID", command=lambda:search(id_search.get()))
button.grid(row=14,column=2)

标签: pythonpostgresqltkinter

解决方案


的第二个参数execute()应该是tupleor list,所以:

cur.execute(query,(id))应该是cur.execute(query,(id,))


推荐阅读