【发布时间】: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中的选定记录。