首页 > 解决方案 > 如何将 MySQL 数据库中的数据填充到 python 中的树视图中?

问题描述

很抱歉,如果我在错误的部分发布了这个,我是 stackoverflow 的新手,我确实搜索过类似的主题,但没有得到我需要的东西。

所以我想做的是,从mysql数据库中获取表的信息,格式为:((1,google,username1,password1),(2,facebook,username2,password2)......)

并将其填写在具有 4 列网站、用户名、密码的树视图中,如下所示:树视图列

我尝试过的是:

#find password
def find_password(selection):
#remove the frame if it already exists
for child in frame.winfo_children():
    child.destroy()

#to fill tree with tuples
def show_table(table):
    for x in table:
        tree.insert("", 0, text=x)


#button1 function
def btn_input_one(text1): 

    text_input = text1.get("1.0", "end-1c")

    connection = mysql.connector.connect(host='localhost',database='suryansh',user='suryansh',password='suryansh')
    if connection.is_connected():
        cursor = connection.cursor(buffered = True)
        cursor.execute("select database();")


        #sql queries
        create_table_query = "select * from " + text_input
        cursor = connection.cursor(buffered = True)
        result = cursor.execute(create_table_query)
        table = cursor.fetchall()
        show_table(table)

这就是我尝试过的,这就是我得到的: 以上结果

标签: mysqlpython-3.xtreeview

解决方案


一位朋友帮我解决了这个问题,解决方案是:

for x in table:
    tree.insert("", "end", text=x[1], values=(x[2], x[3]))

这就是您填充默认列以外的其他列的方式:D


推荐阅读