PYTHON - How To Edit Selected Data In MySQL Database Using Python Tkinter
Project Source Code:
import tkinter as tkfrom tkinter import *from tkinter import ttkimport mysql.connector
root = Tk()root.title("Update Data")
connection = mysql.connector.connect(host='localhost', user='root', password='', port='3306', database='test_py')c = connection.cursor()
bkg = "#ff7675"
frame = tk.Frame(root, bg=bkg)
label_id = tk.Label(frame, text="ID: ", font=('verdana',12), bg=bkg)entry_id = tk.Entry(frame, font=('verdana',12))
label_firstname = tk.Label(frame, text="First Name: ", font=('verdana',12), bg=bkg)entry_firstname = tk.Entry(frame, font=('verdana',12))
label_lastname = tk.Label(frame, text="Last Name: ", font=('verdana',12), bg=bkg)entry_lastname = tk.Entry(frame, font=('verdana',12))
label_email = tk.Label(frame, text="Email: ", font=('verdana',12), bg=bkg)entry_email = tk.Entry(frame, font=('verdana',12))
label_age = tk.Label(frame, text="Age: ", font=('verdana',12), bg=bkg)entry_age = tk.Entry(frame, font=('verdana',12))
def updateData(): user_id = entry_id.get() firstname = entry_firstname.get() lastname = entry_lastname.get() email = entry_email.get() age = entry_age.get()
update_query = "UPDATE `users_2` SET `firstname`=%s,`lastname`=%s,`email`=%s, `age`=%s WHERE `id` =%s " vals = (firstname,lastname,email,age,user_id) c.execute(update_query,vals) connection.commit()
button_update = tk.Button(frame, text="Update", font=('verdana',14),fg='#ffffff', bg='#0984e3', command = updateData)
label_id.grid(row=0, column=0)entry_id.grid(row=0, column=1, pady=10, padx=10)
label_firstname.grid(row=1, column=0)entry_firstname.grid(row=1, column=1, pady=10, padx=10)
label_lastname.grid(row=2, column=0)entry_lastname.grid(row=2, column=1, pady=10, padx=10)
label_email.grid(row=3, column=0, sticky='e')entry_email.grid(row=3, column=1, pady=10, padx=10)
label_age.grid(row=4, column=0, sticky='e')entry_age.grid(row=4, column=1, pady=10, padx=10)
button_update.grid(row=5,column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
frame.grid(row=0, column=0)
root.mainloop()
OUTPUT: