简体   繁体   English

无法从sqlite3数据库填充Tkinter列表框

[英]Can't populate Tkinter listbox from sqlite3 database

I am making a simple Tkinter GUI in python 3.6.6 and i can't get data from a database to populate listbox. 我在python 3.6.6中制作了一个简单的Tkinter GUI,我无法从数据库中获取数据来填充列表框。

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

What is my problem? 我怎么了 When i want to populate listbox with existing data from a sqlite3 database, i click on the "Refresh Data" button and encounter an error. 当我想用来自sqlite3数据库的现有数据填充列表框时,我单击“刷新数据”按钮,遇到错误。 My code should select all rows in a first colon and put everything inside a listbox. 我的代码应选择第一个冒号中的所有行,并将所有内容放入列表框。 Important lines of code are included below: 重要的代码行如下所示:

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")

Every time i click on "Refresh Data" button i get "1: Couldn't Retrieve Data From Database" error. 每次我单击“刷新数据”按钮时,都会收到“ 1:无法从数据库检索数据”错误。 The result of my code should just populate listbox. 我的代码的结果应该只是填充列表框。

Looks like your "name" is just a variable and in every loop is updating his value... so i think you need to move 'self.listOfEntries.insert(name)' inside of the loop. 看起来您的“名称”只是一个变量,并且在每个循环中都在更新他的值...所以我认为您需要在循环内部移动“ self.listOfEntries.insert(name)”。 as you have it only will show the last name in the list. 如您所愿,它只会在列表中显示姓氏。

best, 最好,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM