Excuse write in italian, i'm tryng to do a simple app with GUI with python that allowed to insert and delete record for books anime ecc. I don't know how to do a function to delete records. Someone can help me?
I tried to use the same metode for insert and delete but for delete doesen't works. When i start the program it works regolary but when I press the button to delete semply it doesen't do nothing.
#IMPORTAZIONE MODULI
from cProfile import label
from cgitb import text
from tkinter import *
import sqlite3
from tkinter import messagebox
from tkinter.ttk import Labelframe
from turtle import width
from numpy import delete
from pandas import wide_to_long
#CREAZIONE DATABASE
conn=sqlite3.connect('MangaManager.db')
#creazione del cursone
c=conn.cursor()
#CREAZIONE TABELLA
'''c.execute("""CREATE TABLE Record(
Titolo text,
Tipo text,
Valutazione int
)""")'''
#SEZIONE DEFINIZIONI FUNZIONI
#FUNZIONE INSERT
def insert():
conn=sqlite3.connect('MangaManager.db')
c=conn.cursor()
if Entrate[0].get()!="" and Entrate[1].get()!="" and Entrate[2].get()!="":
c.execute("""INSERT INTO Record(Titolo, Tipo, Valutazione)
VALUES (:Titolo, :Tipo, :Valutazione)""",
{
'Titolo':Entrate[0].get(),
'Tipo':Entrate[1].get(),
'Valutazione':Entrate[2].get()
})
else:
messagebox.showerror("Errore inserimento", "Non hai compilato uno dei campi, non è permesso lasciarne vuoti")
conn.commit()
conn.close()
Entrate[0].delete(0, END)
Entrate[1].delete(0, END)
Entrate[2].delete(0, END)
#FUNZIONE RECUPERA REPORT
def RecuperaReport():
conn=sqlite3.connect('MangaManager.db')
#creazione del cursone
c=conn.cursor()
c.execute('''SELECT *, oid FROM record''')
reports=c.fetchall()
lista=[]
for record in reports:
lista.append(record[0] ' ' record[1] ' ' str(record[2]))
conn.commit()
conn.close()
return lista
def delete():
conn=sqlite3.connect('MangaManager.db')
#creazione del cursone
c=conn.cursor()
c.execute('''DELETE FROM record WHERE Titolo=:aaa''',
{
'aaa':Entrate[3].get()
}
)
#SEZIONE CREAZIONE INTEFRACCIA GRAFICA
#CREAZIONE FINESTRA
f=Tk()
f.title("MyMangAndAnimeManager")
f.geometry("560x560")
#CREAZIONE TITOLO
titolo=Label(f, text="MyMangaAndAnimeManager")
titolo.grid(row=0 ,column=0,columnspan=3, sticky=E W)
#CREAZIONE BLOCCHI ENTRATA
Entrate=[]
for i in range(0, 3):
entry=Entry(f, width=27)
entry.grid(column=i, row=1, padx=(10, 0))
Entrate.append(entry)
#CREAZIONE ENTRY PER LA CANCELLAZIONE
EtichettaCanc=Label(f, text="Inserire il titolo da cancellare", width=27)
EtichettaCanc.grid(row=5, column=0, columnspan=3, pady=(10,0))
entry=Entry(f, width=27)
Entrate.append(entry)
Entrate[3].grid(row=6, column=0, columnspan=3, pady=(10,0))
BottoneCanc=Button(f, text="Premere per cancellare", command=delete, width=27)
BottoneCanc.grid(row=8, column=0, columnspan=3, pady=(10,0))
#CREAZIONE CONTENITORE LISTA ANIME
Contenitore=Labelframe(f, width=50, height=200)
Contenitore.grid(row=4 ,column=0, pady=(10,0), columnspan=3, sticky=W, padx=(10,0))
for l in RecuperaReport():
etichetta=Label(Contenitore, text=l)
etichetta.pack(side=BOTTOM)
#CREAZIONE BOTTONE ENTRATA DATI
B=Button(f, text="Premere per inserire", command=insert, width=27)
B.grid(row=3 ,column=1, pady=10)
#CREAZIONE BOTTONE USCITA DATI
#B2=Button(f, text="Premere per far vedere i dati", command=RecuperaReport, width=27)
#B2.grid(row=5 ,column=1, pady=10)
#FINE DEL LOOP
f.mainloop()
CodePudding user response:
Make sure to close and write to the database after making an edit with .commit()
CodePudding user response:
The cursor.execute(query) method executes the operation stored in the delete query.
def deleteRecord():
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
# Deleting single record now
sql_delete_query = """DELETE from SqliteDb_developers where id = 6"""
cursor.execute(sql_delete_query)
sqliteConnection.commit()
print("Record deleted successfully ")
cursor.close()
except sqlite3.Error as error:
print("Failed to delete record from sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("the sqlite connection is closed")
deleteRecord()