Online-Academy
Look, Read, Understand, Apply

CRUD-DB-UI

Reading and inserting records to database using User Interface

  • Database Name: sawadee
  • Table name: customers
  • Attributes of customers table : cname and mobile
  • This program has a main window. Main window has menu, as menu is clicked two options will appear Create and Read. If create is clicked new window will be opened using which new records can be inserted to database. If read click window is clicked new window will be opened with a read button, as read button is clicked records from database will be displayed.

    import tkinter as tk
    import mysql.connector
    from tkinter import messagebox
    
    mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        password="",
        database="sawadee",
    )
    mycursor = mydb.cursor()
    
    def read_records():  #function 
        def read():        #inner function
            sql = "select cname,mobile from customers"
            mycursor.execute(sql)
            results = mycursor.fetchall()
            j = 1
            for i in results:
                k = 0
                for x in i:
                    lblname = tk.Label(showWindow,text=x,width=10)
                    lblname.grid(row=j, column=k)
                    k = k + 1
                j = j + 1
        showWindow = tk.Toplevel(root)
        showWindow.title("Read Records")
        showWindow.resizable(width=False, height=False)
        showWindow.geometry("400x300")
        showButton = tk.Button(showWindow, text="Read", command=read)
        showButton.grid(row=0, column=0)
    
    
    def create_record():  #function 
        def save():     #inner function 
            name = name_entry.get()
            mobile = mobile_entry.get()
    
            if name and mobile:
                sql = "insert into customers(cname,mobile) values(%s,%s)"
                vals = (name, mobile)
                mycursor.execute(sql, vals)
                mydb.commit()
                messagebox.showinfo("Success", "Record added!")
                window.destroy()
            else:
                messagebox.showwarning("Error", "All fields required!")
    
        window = tk.Toplevel(root)
        window.title("Create Record")
        window.geometry("400x300")
        window.resizable(width=False, height=False)
        tk.Label(window, text="Name").grid(row=0, column=0)
        name_entry = tk.Entry(window)
        name_entry.grid(row=0, column=1)
        tk.Label(window, text="Age").grid(row=1, column=0)
        mobile_entry = tk.Entry(window)
        mobile_entry.grid(row=1, column=1)
    
        tk.Button(window, text="Save", command=save).grid(row=2, column=1)
    
    # ------------------ MAIN WINDOW ------------------
    
    root = tk.Tk()
    root.title("CRUD Menu Driven App")
    root.geometry("500x400")
    
    # Menu Bar
    menu = tk.Menu(root)
    root.config(menu=menu)
    
    crud_menu = tk.Menu(menu, tearoff=0)
    menu.add_cascade(label="CRUD Operations", menu=crud_menu)
    
    crud_menu.add_command(label="Create", command=create_record)
    crud_menu.add_command(label="Read", command=read_records)
    
    # Run app
    root.mainloop()