Dbms Ex 12
Dbms Ex 12
NO:12
GUI based database application
Date:30.05.24
Aim:
To Develop a simple GUI based database application by using previous experiment.
Algorithm:
1. Establish a database connection using MySQL credentials.
2. Execute SQL to add DepartmentID column to Employees table.
3. Import necessary modules and create the main Tkinter window.
4. Create and place entry fields for EmployeeID, FirstName, LastName, Email, Age, and
DepartmentID.
5. Define a function to add an employee by inserting data into the database.
6. Define a function to update an employee by modifying data in the database based on EmployeeID.
7. Define a function to delete an employee by removing data from the database based on EmployeeID.
8. Define a function to display all employee data by retrieving it from the database.
9. Create and place buttons for adding, updating, and deleting employees, linking them to their
respective functions.
10. Setup a Treeview widget to display employee data and call the display function to populate it
initially.
MySql Commands:
create database GUI;
use GUI;
create table employees(Employee ID int not null,
First Name varchar(20) ,
Last Name varchar(20),
Email ID varchar(30),
Age int ,
Department ID int not null
);
Program:
import tkinter as tk
from tkinter import messagebox, ttk
import mysql.connector
# Database connection
def connect_db():
return mysql.connector.connect(
host="localhost",
user="root",
password="name@1234",
database="GUI"
)
# Function to add an employee to the database
def add_employee():
try:
mydb = connect_db()
mycursor = mydb.cursor()
sql = "INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, Age, DepartmentID) VALUES
(%s, %s, %s, %s, %s, %s)"
val = (entry_id.get(), entry_first_name.get(), entry_last_name.get(), entry_email.get(), entry_age.get(),
entry_department_id.get())
mycursor.execute(sql, val)
mydb.commit()
messagebox.showinfo("Success", "Employee added successfully")
display_data()
except Exception as e:
messagebox.showerror("Error", str(e))
finally:
mydb.close()
# Function to update employee details
def update_employee():
try:
mydb = connect_db()
mycursor = mydb.cursor()
sql = "UPDATE Employees SET FirstName=%s, LastName=%s, Email=%s, Age=%s, DepartmentID=%s
WHERE EmployeeID=%s"
val = (entry_first_name.get(), entry_last_name.get(), entry_email.get(), entry_age.get(),
entry_department_id.get(), entry_id.get())
mycursor.execute(sql, val)
mydb.commit()
messagebox.showinfo("Success", "Employee updated successfully")
display_data()
except Exception as e:
messagebox.showerror("Error", str(e))
finally:
mydb.close()
# Function to delete an employee from the database
def delete_employee():
try:
mydb = connect_db()
mycursor = mydb.cursor()
sql = "DELETE FROM Employees WHERE EmployeeID=%s"
val = (entry_id.get(),)
mycursor.execute(sql, val)
mydb.commit()
messagebox.showinfo("Success", "Employee deleted successfully")
display_data()
except Exception as e:
messagebox.showerror("Error", str(e))
finally:
mydb.close()
# Function to display employee data
def display_data():
try:
mydb = connect_db()
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Employees")
rows = mycursor.fetchall()
tree.delete(*tree.get_children())
for row in rows:
tree.insert("", tk.END, values=row)
except Exception as e:
messagebox.showerror("Error", str(e))
finally:
mydb.close()
# GUI setup
root = tk.Tk()
root.title("Employee Management")
# Form labels and entries
tk.Label(root, text="Employee ID").grid(row=0, column=0)
tk.Label(root, text="First Name").grid(row=1, column=0)
tk.Label(root, text="Last Name").grid(row=2, column=0)
tk.Label(root, text="Email").grid(row=3, column=0)
tk.Label(root, text="Age").grid(row=4, column=0)
tk.Label(root, text="Department ID").grid(row=5, column=0)
entry_id = tk.Entry(root)
entry_first_name = tk.Entry(root)
entry_last_name = tk.Entry(root)
entry_email = tk.Entry(root)
entry_age = tk.Entry(root)
entry_department_id = tk.Entry(root)
entry_id.grid(row=0, column=1)
entry_first_name.grid(row=1, column=1)
entry_last_name.grid(row=2, column=1)
entry_email.grid(row=3, column=1)
entry_age.grid(row=4, column=1)
entry_department_id.grid(row=5, column=1)
# Buttons
btn_add = tk.Button(root, text="Add Employee", command=add_employee)
btn_add.grid(row=6, column=0)
btn_update = tk.Button(root, text="Update Employee", command=update_employee)
btn_update.grid(row=6, column=1)
btn_delete = tk.Button(root, text="Delete Employee", command=delete_employee)
btn_delete.grid(row=6, column=2)
# Treeview for displaying data
tree = ttk.Treeview(root, columns=("EmployeeID", "FirstName", "LastName", "Email", "Age", "DepartmentID"),
show='headings')
tree.heading("EmployeeID", text="EmployeeID")
tree.heading("FirstName", text="FirstName")
tree.heading("LastName", text="LastName")
tree.heading("Email", text="Email")
tree.heading("Age", text="Age")
tree.heading("DepartmentID", text="DepartmentID")
tree.grid(row=7, column=0, columnspan=3)
# Display data initially
display_data()
root.mainloop()
OUTPUT:
Result:
Thus the simple GUI based database application by using previous experiments is executed successfully.