【问题标题】:Can't populate Tkinter listbox from sqlite3 database无法从 sqlite3 数据库填充 Tkinter 列表框
【发布时间】:2019-01-05 00:55:15
【问题描述】:

我正在 python 3.6.6 中制作一个简单的 Tkinter GUI,但我无法从数据库中获取数据来填充列表框。

图片:https://imgur.com/sMou0MQ

我的问题是什么?当我想用来自 sqlite3 数据库的现有数据填充列表框时,我单击“刷新数据”按钮并遇到错误。 我的代码应该选择第一个冒号中的所有行并将所有内容放在列表框中。 重要的代码行如下:

db_conn = sqlite3.connect("dbs/entries.db")
cursor = db_conn.cursor()

def updateListbox(self):
    #Delete items in the list box
    self.listOfEntries.delete(0, "END")

    #Get users from the db
    try:

        result = self.cursor.execute("SELECT Name FROM Entries")

        # Receive a list of lists that hold the result
        for row in result.fetchall():
            name = row[0]


        # Put the student in the list box
        self.listOfEntries.insert(name)

    except sqlite3.OperationalError:
        print("The Table Doesn't Exist")

    except:
        print("1: Couldn't Retrieve Data From Database")

每次单击“刷新数据”按钮时,我都会收到“1:无法从数据库中检索数据”错误。 我的代码的结果应该只是填充列表框。

【问题讨论】:

  • 暂时删除except 子句或在其中添加traceback.print_exc() 以获取更多错误详情。如果您需要进一步的帮助,请编辑问题以显示回溯。
  • insert 方法需要两个参数。

标签: python tkinter sqlite


【解决方案1】:

看起来你的“名字”只是一个变量,在每个循环中都在更新他的值...... 所以我认为你需要在循环内移动'self.listOfEntries.insert(name)'。 正如你所拥有的那样,它只会在列表中显示姓氏。

最好的,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    相关资源
    最近更新 更多