Deleting record from MySQL table using Treeview Selection


MySQL: Delete Records in Treeview
Connect to MySQL database display rows from MySQL.
Read our tutorial on how to display MySQL records in Treeview.
We will add delete feature to this.


  • Delete Selected row from Treeview & from MySQL database

Reading selected row number or iid of the Treeview.
selected_item = trv.selection()[0]
Deleting selected record from MySQL table
query="DELETE FROM  student WHERE id=%s"
rs=my_conn.execute(query,selected_item)
After deleting we will ensure that the rs.rowcount is equal to 1. This will confirm that only one record is deleted. Based on this value we will delete the row from the Treeview.
if(rs.rowcount==1):
            trv.delete(selected_item)
We can manage the message based on the outcome of the delete operation.
With Tkinter - Treeview the Full code is here
from tkinter import ttk
import tkinter as tk
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
r_set=my_conn.execute('SELECT * FROM student LIMIT 0,10')
my_w=tk.Tk()
my_w.geometry('400x350')
my_w.title("www.plus2net.com")
trv=ttk.Treeview(my_w,selectmode='browse')
trv.grid(row=1,column=1,padx=20,pady=20)
trv["columns"]=("1","2","3","4","5")
trv['show']='headings'
#trv['show']='tree'
trv.column("1",width=30,anchor='c')
trv.column("2",width=80,anchor='c')
trv.column("3",width=80,anchor='c')
trv.column("4",width=80,anchor='c')
trv.column("5",width=80,anchor='c')
trv.heading("1",text="id")
trv.heading("2",text="Name")
trv.heading("3",text="Class")
trv.heading("4",text="Mark")
trv.heading("5",text="Gender")

for dt in r_set:
    #print(dt)
    trv.insert("",'end',iid=dt[0],
		values=(dt[0],dt[1],dt[2],dt[3],dt[4]))

b1 = tk.Button(my_w, text='delete row', width=20,bg='yellow',
    command=lambda: delete())
b1.grid(row=2,column=1)
my_font=('times', 10, '')
my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str, font=my_font)
l1.config(fg='blue')
l1.grid(row=3,column=1)
my_str.set("Message here")#print(my_error)
def delete():
    selected_item = trv.selection()[0] ## get selected item
    try:
        query="DELETE FROM  student WHERE id=%s"
        rs=my_conn.execute(query,selected_item)
        if(rs.rowcount==1):
            trv.delete(selected_item) #delete from Treeview
            l1.config(fg='green') # message font colour
            my_str.set("Record deleted")#Message 
    except SQLAlchemyError as e:
        error=str(e.__dict__['orig'])
        l1.config(fg='red')
        my_str.set(error)        
    l1.after(3000, lambda: my_str.set('') ) # remove the message
l1.after(3000, lambda: my_str.set('') )
my_w.mainloop()
Display MySQL records in Treeview Pagination of MySQL records in Treeview
Displaying MySQL records using Entry or Label Dynamic Creation of Header & Columns in Treeview
Delete MySQL record Select row in Treeview to Delete from Pandas DataFrame Treeview Treeview insert Treeview parent child node Select -Edit-update MySQL Product table using Treeview
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer