Document 16 1
Document 16 1
USE?
1.USE MYSQL TO USE THE MYSQL
CODE.
2.USE THE PYTHON CODE WITH THE
MYSQL CODE TO FIND THE OUTPUT.
MySQL
Code
CREATE DATABASE contact_manager;
USE contact_manager;
Summary
This code sets up a basic structure for
a contact management system,
allowing users to store and manage
contact information efficiently. The
use of AUTO_INCREMENT for
contact_id ensures that each contact
has a unique identifier, while the
created_at timestamp provides a
record of when each contact was
added.
Python
Code
import tkinter as tk
from tkinter import messagebox
import pymysql
conn = pymysql.connect(
host="localhost",
user="root",
password="root",
database="contact_manager"
)
cursor = conn.cursor()
def add_contact():
name = entry_name.get()
phone = entry_phone.get()
email = entry_email.get()
if name and phone:
cursor.execute("INSERT INTO
Contacts (name, phone, email) VALUES
(%s, %s, %s)", (name, phone, email))
conn.commit()
load_contacts()
entry_name.delete(0, tk.END)
entry_phone.delete(0, tk.END)
entry_email.delete(0, tk.END)
else:
messagebox.showwarning("Input
Error", "Name and Phone are required.")
def load_contacts():
listbox_contacts.delete(0, tk.END)
cursor.execute("SELECT * FROM
Contacts")
for contact in cursor.fetchall():
listbox_contacts.insert(tk.END,
f"{contact[0]} - {contact[1]} |
{contact[2]} | {contact[3]}")
root = tk.Tk()
root.title("Contact Management System")
tk.Label(root, text="Name").pack()
entry_name = tk.Entry(root)
entry_name.pack()
tk.Label(root, text="Phone").pack()
entry_phone = tk.Entry(root)
entry_phone.pack()
tk.Label(root, text="Email").pack()
entry_email = tk.Entry(root)
entry_email.pack()
tk.Button(root, text="Add Contact",
command=add_contact).pack(pady=5)
listbox_contacts = tk.Listbox(root,
width=50, height=10)
listbox_contacts.pack(pady=10)
load_contacts()
root.mainloop()
Slide 1: Overview
Purpose: A simple Contact Management
System that allows users to add and view
contacts.
Technologies Used:
o Tkinter: For the graphical user
interface (GUI).
o pymysql: For connecting to a MySQL
database.
Slide 2: Database Connection
Connection Setup:
o Connects to a MySQL database named
contact_manager.
o Uses credentials:
Host: localhost
User: root
Password: root
Cursor Creation: A cursor is created to
execute SQL commands.
Slide 3: Adding Contacts
Function: add_contact()
o Retrieves user input for name, phone,
and email.
o Validates that name and phone are
provided.
o Executes an SQL INSERT command to
add the contact to the Contacts table.
o Clears input fields after successful
addition.
o Displays a warning if required fields
are missing.
Slide 4: Loading Contacts
Function: load_contacts()
o Clears the current list of contacts in
the GUI.
o Executes an SQL SELECT command to
fetch all contacts from the Contacts
table.
o Displays each contact in a listbox in
the format: ID - Name | Phone |
Email.
Slide 5: User Interface
Components:
o Labels and Entry fields for Name,
Phone, and Email.
o A button to trigger the addition of a
new contact.
o A Listbox to display the list of contacts.
Main Loop: The application runs in a loop,
waiting for user interactions.
Output
OTHER CONTACT
MANAGEMENT APPS