简体   繁体   English

如何从 tkinter treeview 中删除记录以应用 sqlite3 中的更改

[英]How to delete record from tkinter treeview to apply the changes in sqlite3

Am trying to delete record displayed in tkinter treeview by using the tree.selection() method.我正在尝试使用tree.selection()方法删除tree.selection() tkinter treeview显示的记录。 I want the selected row in the treeview selected to also be deleted from the sqlite3 db also but when i select row to be deleted i received this error我希望treeview选定的row也从sqlite3 db 中删除,但是当我选择要删除的row ,我收到此错误

_tkinter.TclError: Item I not found _tkinter.TclError:我没有找到项目

i found this link on the site but i doesn't answer my talent.我在网站上找到了这个链接,但我没有回答我的才能。

from tkinter import ttk
import tkinter as tk
import sqlite3


def connect():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, 
First TEXT, Surname TEXT)")
    conn.commit()
    conn.close()


def Delete():
    selected_item = tree.selection()[0]

    print(selected_item)  # it prints the selected row id

    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    cur.execute("DELETE FROM profile WHERE id=?", (selected_item,))
    conn.commit()

    for b in selected_item:
        tree.delete(b)

    conn.close()


connect()  #  this to create the db

root = tk.Tk()
root.geometry("400x400")

tree= ttk.Treeview(root, column=("column1", "column2", "column3"), 
show='headings')
tree.heading("#1", text="NUMBER") 
tree.heading("#2", text="FIRST NAME")
tree.heading("#3", text="SURNAME")
tree.pack()


b2 = tk.Button(text="DELETE FROM PARTICULAR DATA TREEVIEW AND SQLITE3", 
command=Delete)
b2.pack(side=tk.BOTTOM)

root.mainloop()

There are three problems in your Delete function:您的Delete函数存在三个问题:

  • First, you do首先,你做

    for b in selected_item: tree.delete(b)

    but selected_item is a string, so you iterate over the characters of the item id.但是selected_item是一个字符串,因此您可以遍历项目 ID 的字符。 I think that what you wanted to do was to iterate over the selected items, something like:我认为您想要做的是迭代所选项目,例如:

     for selected_item in tree.selection(): tree.delete(selected_item)
  • Secondly, you try to delete the db entry with其次,您尝试删除 db 条目

    cur.execute("DELETE FROM profile WHERE id=?", (selected_item,))

    but selected_item is the item id in the tree, not in the db.selected_item是树中的项目 ID,而不是数据库中的项目 ID。 The db id is the one in the first column and you can get it with db id 是第一列中的那个,你可以用

    tree.set(selected_item, '#1')
  • Finally, the line selected_item = tree.selection()[0] is going to throw an error if there is no selected item, but if what you want is to remove all selected items, then you no longer need this line (see code below).最后,如果没有选定的项目,则selected_item = tree.selection()[0]抛出错误,但是如果您想要删除所有选定的项目,那么您不再需要此行(请参阅下面的代码)。

Here is the full function:这是完整的功能:

def Delete():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    for selected_item in tree.selection():
        print(selected_item)  # it prints the selected row id
        cur.execute("DELETE FROM profile WHERE id=?", (tree.set(selected_item, '#1'),))
        conn.commit()
        tree.delete(selected_item)
    conn.close()

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

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