首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建搜索记录函数Python SQLite3

创建搜索记录函数Python SQLite3
EN

Stack Overflow用户
提问于 2021-01-09 07:19:49
回答 1查看 30关注 0票数 0

我目前正在为学校的一个课程作业项目工作,它是一个数据库系统,使用Tkinter,Python和SQLite3的用户界面。我已经制作了一个表单来添加、删除、更新和搜索客户。我能够显示单个字段的结果,但是,我正在努力让消息框显示所有字段,这正是我希望它做的事情。我有随附的形式与代码的照片。提前谢谢你。

代码语言:javascript
运行
复制
    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])

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-09 11:14:51

这是因为您只显示了result中的第一列(i[0])。

由于特定客户ID应该只有一条记录,因此您应该使用fetchone()而不是fetchall(),然后您可以显示整个记录,如下所示:

代码语言:javascript
运行
复制
    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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65637682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档