python - 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)
解决方案
的第二个参数execute()
应该是tuple
or list
,所以:
cur.execute(query,(id))
应该是cur.execute(query,(id,))
。
推荐阅读
- c# - 如何在场景中自动移动相机?
- php - 如何只在字符串中查找子字符串?
- javascript - 如何在javascript中使用正则表达式捕获以某种字符串开头和结尾的字符串
- angular-material - 无法绑定到“dateClass”,因为它不是 angular7 中“mat-datepicker”的已知属性
- c# - 在使用 app.UseRewriter().Add() 添加重定向时访问 Startup.Configure 中的 dbcontext
- git - 关闭显示“进程正在会话中运行”的 git 控制台时如何解决问题
- r - 在 R 的循环中基于 ID 约束对所有组合进行采样
- javascript - Javascript SyntaxError:重新声明类
- javascript - 将 ref 传递给 HTML 元素到自定义钩子
- javascript - 在 Nodejs 中处理异步