【问题标题】:Double insert into listbox SQLITE3 & TKINTER双重插入到列表框 SQLITE3 和 TKINTER
【发布时间】:2021-07-13 02:48:44
【问题描述】:

我的程序有问题。当我点击保存按钮时,我输入的第一个数据是正确的。但是当我插入新数据时,第一个数据被复制到我的列表框中,然后新的数据也被插入。

我认为问题就在这里:

def save_register():
    firstname = entry_firstname.get()
    name = entry_name.get()
    adress = entry_adress.get()
    phone = entry_phone.get()
    connection = sql3.connect("gestionnaire.db")
    cursor = connection.cursor()
    cursor.execute("INSERT INTO informations ('FirstName', 'Name', 'Adress', 'Phone') VALUES (?,?,?,?)", (firstname, name, adress, phone))
    connection.commit()
    rows = cursor.execute("SELECT * FROM informations")
    for row in rows:
        list_1.insert(END, row) #TODO insertion en double

所有代码:

from tkinter import *
import sqlite3 as sql3

#FONCTIONS
def create_table_and_database():
    connection = sql3.connect("gestionnaire.db")
    cursor = connection.cursor()
    cursor.execute("""CREATE TABLE IF NOT EXISTS informations(
                      id INTEGER PRIMARY KEY AUTOINCREMENT, 
                      FirstName TEXT NOT NULL,
                      Name TEXT NOT NULL,
                      Adress TEXT NOT NULL,
                      Phone TEXT NOT NULL)             
    """)

def save_register():
    firstname = entry_firstname.get()
    name = entry_name.get()
    adress = entry_adress.get()
    phone = entry_phone.get()
    connection = sql3.connect("gestionnaire.db")
    cursor = connection.cursor()
    cursor.execute("INSERT INTO informations ('FirstName', 'Name', 'Adress', 'Phone') VALUES (?,?,?,?)", (firstname, name, adress, phone))
    connection.commit()
    rows = cursor.execute("SELECT * FROM informations")
    for row in rows:
        list_1.insert(END, row) #TODO insertion en double

def delete_register():
    index = list_1.curselection()
    connection = sql3.connect("gestionnaire.db")
    cursor = connection.cursor()
    for row in index:
        cursor.execute("DELETE FROM informations")
        list_1.delete(row)
    connection.commit()

def refresh_insert():
    connection = sql3.connect("gestionnaire.db")
    cursor = connection.cursor()
    rows = cursor.execute("SELECT * FROM informations")
    for row in rows:
        list_1.insert(END, row)  # TODO insertion en double
    connection.commit()

def new_registers():
    entry_name.delete(0, END)
    entry_firstname.delete(0, END)
    entry_adress.delete(0, END)
    entry_phone.delete(0, END)

def get_list(event):
    index = list_1.curselection()
    seltext = list_1.get(index)[1]
    seltext_2 = list_1.get(index)[2]
    seltext_3 = list_1.get(index)[3]
    seltext_4 = list_1.get(index)[4]
    entry_firstname.delete(0, 50)
    entry_name.delete(0, 50)
    entry_adress.delete(0, 50)
    entry_phone.delete(0, 50)
    entry_firstname.insert(0, seltext)
    entry_name.insert(0, seltext_2)
    entry_adress.insert(0, seltext_3)
    entry_phone.insert(0, seltext_4)



if __name__ == "__main__":
    create_table_and_database()


# BASE WINDOW

root = Tk()
root.title("Gestionnaire d'adresses")
root.geometry("700x300")
root.resizable(width=0, height=0)

# LISTBOX
list_1 = Listbox(root, height = 100, width = 23)
list_1.pack(side = LEFT, fill = BOTH)
list_1.bind('<ButtonRelease-1>', get_list)

# LABEL, ENTRY
# FIRST NAME
lbl_firstname = Label(root,text = "Nom :")
lbl_firstname.place(x =200, y = 50)
entry_firstname = Entry(root)
entry_firstname.place(x = 300, y = 50, width = 390)

# NAME
lbl_name = Label(root,text = "Prénom :")
lbl_name.place(x =200, y = 100)
entry_name = Entry(root)
entry_name.place(x = 300, y = 100, width = 390)

# ADRESS
lbl_adress = Label(root,text = "Adresse :")
lbl_adress.place(x =200, y = 150)
entry_adress = Entry(root)
entry_adress.place(x = 300, y = 150, width = 390)

# PHONE
lbl_phone = Label(root,text = "Téléphone :")
lbl_phone.place(x =200, y = 200)
entry_phone = Entry(root)
entry_phone.place(x = 300, y = 200, width = 390)


# BUTTON

# NEW
button_new = Button(root, text="Nouveau", command = new_registers)
button_new.pack()
button_new.place(x = 300, y = 250)

# SAVE
button_save = Button(root, text="Enregistrer", command = save_register)
button_save.pack()
button_save.place(x = 400, y = 250)

# DELETE
button_delete = Button(root, text="Supprimer", command = delete_register)
button_delete.pack()
button_delete.place(x = 500, y = 250)

# QUIT
button_quit = Button(root, text="Quitter", command = quit)
button_quit.pack()
button_quit.place(x = 600, y = 250)

refresh_insert()
root.mainloop()

【问题讨论】:

  • 您是否尝试使用关注rows = cursor.fetchall() 而不是rows = cursor.execute("SELECT * FROM informations")
  • 在插入来自 SQL 查询的数据之前,您需要清除列表框。
  • delete_register() 将清除表informations,而不仅仅是删除list_1 中的选定记录。

标签: python sqlite tkinter


【解决方案1】:

按照https://pynative.com/python-sqlite-insert-into-table/,也可以使用如下:

rows = cursor.fetchall() 而不是rows = cursor.execute("SELECT * FROM informations") ?

然后再用cursor.close()关闭光标对象。

通常,在您的 CUD 操作周围使用 try-catch 块 也很好,这样您就可以处理检查的异常。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    这是因为您在从表中传播数据之前忘记清除列表框list_1

    def save_register():
        firstname = entry_firstname.get()
        name = entry_name.get()
        adress = entry_adress.get()
        phone = entry_phone.get()
        connection = sql3.connect("gestionnaire.db")
        cursor = connection.cursor()
        cursor.execute("INSERT INTO informations ('FirstName', 'Name', 'Adress', 'Phone') VALUES (?,?,?,?)", (firstname, name, adress, phone))
        connection.commit()
        list_1.delete(0, "end") # clear list_1
        rows = cursor.execute("SELECT * FROM informations")
        for row in rows:
            list_1.insert(END, row)
    

    或者只是将新记录附加到list_1

    def save_register():
        firstname = entry_firstname.get()
        name = entry_name.get()
        adress = entry_adress.get()
        phone = entry_phone.get()
        connection = sql3.connect("gestionnaire.db")
        cursor = connection.cursor()
        cursor.execute("INSERT INTO informations ('FirstName', 'Name', 'Adress', 'Phone') VALUES (?,?,?,?)", (firstname, name, adress, phone))
        connection.commit()
        # insert new record into list_1
        list_1.insert(END, (cursor.lastrowid, firstname, name, adress, phone))
    

    delete_register() 中还有另一个问题:它将删除informations 表中的所有记录,而不是list_1 中的选定记录。您需要在DELETE SQL 语句中添加WHERE 子句:

    def delete_register():
        index = list_1.curselection()
        if index:
            recid = list_1.get(index)[0]  # get the unique ID of the selected record
            connection = sql3.connect("gestionnaire.db")
            cursor = connection.cursor()
            for row in index:
                cursor.execute("DELETE FROM informations WHERE id = ?", (recid,)) # delete selected record
                list_1.delete(row)
            connection.commit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 2019-01-30
      • 2012-01-24
      相关资源
      最近更新 更多