我目前正在为学校的一个课程作业项目工作,它是一个数据库系统,使用Tkinter,Python和SQLite3的用户界面。我已经制作了一个表单来添加、删除、更新和搜索客户。我能够显示单个字段的结果,但是,我正在努力让消息框显示所有字段,这正是我希望它做的事情。我有随附的形式与代码的照片。提前谢谢你。
def SearchCustomer(self):
customerid = self.CustomerEntry.get();
with sqlite3.connect("LeeOpt.db") as db:
cursor = db.cursor()
search_customer = ('''SELECT * FROM Customer WHERE CustomerID = ?''')
cursor.execute(search_customer, [(customerid)])
results = cursor.fetchall()
if results:
for i in results:
tkinter.messagebox.showinfo("Notification",i[0])
发布于 2021-01-09 11:14:51
这是因为您只显示了result中的第一列(i[0]
)。
由于特定客户ID应该只有一条记录,因此您应该使用fetchone()
而不是fetchall()
,然后您可以显示整个记录,如下所示:
def SearchCustomer(self):
customerid = self.CustomerEntry.get()
with sqlite3.connect("LeeOpt.db") as db:
cursor = db.cursor()
search_customer = "SELECT * FROM Customer WHERE CustomerID = ?"
cursor.execute(search_customer, [customerid])
result = cursor.fetchone() # there should be only one record for specific customer ID
if result:
tkinter.messagebox.showinfo("Notification", "\n".join(str(x) for x in result))
https://stackoverflow.com/questions/65637682
复制相似问题