py12
py12
12
Program to demonstrate CRUD (create, read, update and
delete) operations on database (SQLite/ MySQL) using
python
Date of Performance:28/3/25
Date of Submission:
Experiment No. 12
Title: Program to demonstrate CRUD (create, read, update and delete) operations on database
(SQLite/ MySQL) using python
Aim: To study and implement CRUD (create, read, update and delete) operations on database
(SQLite/ MySQL) using python
Theory:
In general CRUD means performing Create, Retrieve, Update and Delete operations on a table in
a database. Let’s discuss what actually CRUD means,
PythonProject.py
import tkinter as tk
from tkinter import ttk, messagebox
import re
from database import PasswordDatabase
class PasswordManager:
def __init__(self):
self.db = PasswordDatabase()
self.setup_gui()
def setup_gui(self):
self.root = tk.Tk()
self.root.title("Password Manager")
self.root.geometry("600x400")
# Create tabs
self.add_tab = ttk.Frame(self.notebook)
self.view_tab = ttk.Frame(self.notebook)
self.notebook.add(self.add_tab, text="Add Password")
self.notebook.add(self.view_tab, text="View Passwords")
def setup_add_tab(self):
# Website entry
ttk.Label(self.add_tab, text="Website:").pack(pady=5)
self.website_entry = ttk.Entry(self.add_tab)
self.website_entry.pack(pady=5)
# Username entry
ttk.Label(self.add_tab, text="Username:").pack(pady=5)
self.username_entry = ttk.Entry(self.add_tab)
self.username_entry.pack(pady=5)
# Password entry
ttk.Label(self.add_tab, text="Password:").pack(pady=5)
self.password_entry = ttk.Entry(self.add_tab, show="*")
self.password_entry.pack(pady=5)
# Add button
ttk.Button(self.add_tab, text="Add Password",
command=self.add_password).pack(pady=10)
# Status label
self.status_label = ttk.Label(self.add_tab, text="")
self.status_label.pack()
def setup_view_tab(self):
# Create Treeview
self.tree = ttk.Treeview(self.view_tab, columns=("Website", "Username", "Password"),
show="headings")
self.tree.heading("Website", text="Website")
self.tree.heading("Username", text="Username")
self.tree.heading("Password", text="Password")
self.tree.pack(pady=10, padx=10, fill="both", expand=True)
# Add scrollbar
scrollbar = ttk.Scrollbar(self.view_tab, orient="vertical", command=self.tree.yview)
scrollbar.pack(side="right", fill="y")
self.tree.configure(yscrollcommand=scrollbar.set)
# Buttons frame
button_frame = ttk.Frame(self.view_tab)
button_frame.pack(pady=10)
# Add buttons
ttk.Button(button_frame, text="Refresh",
command=self.refresh_passwords).pack(side="left", padx=5)
ttk.Button(button_frame, text="Delete Selected",
command=self.delete_selected).pack(side="left", padx=5)
def toggle_password(self):
if self.show_password_var.get():
self.password_entry.config(show="")
else:
self.password_entry.config(show="*")
def add_password(self):
website = self.website_entry.get()
username = self.username_entry.get()
password = self.password_entry.get()
def refresh_passwords(self):
# Clear existing items
for item in self.tree.get_children():
self.tree.delete(item)
# Get passwords from database
passwords = self.db.get_all_passwords()
for password in passwords:
# If show_stored_passwords is True, show actual password, else show asterisks
display_password = password[3] if self.show_stored_passwords.get() else "********"
self.tree.insert("", "end", values=(password[1], password[2], display_password))
def delete_selected(self):
selected_item = self.tree.selection()
if not selected_item:
messagebox.showwarning("Warning", "Please select a password to delete")
return
def run(self):
self.root.mainloop()
if __name__ == "__main__":
app = PasswordManager()
app.run()
database.py:
import mysql.connector
from mysql.connector import Error
class PasswordDatabase:
def __init__(self):
try:
# First connect without database
self.connection = mysql.connector.connect(
host='localhost',
user='root',
password='Lakshya@4321'
)
self.create_database()
# Then connect to the created database
self.connection = mysql.connector.connect(
host='localhost',
user='root',
password='Lakshya@4321',
database='password_manager'
)
self.create_table()
except Error as e:
print(f"Error connecting to MySQL: {e}")
self.connection = None
def create_database(self):
try:
cursor = self.connection.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS password_manager")
cursor.execute("USE password_manager")
self.connection.commit()
except Error as e:
print(f"Error creating database: {e}")
def create_table(self):
try:
cursor = self.connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS passwords (
id INT AUTO_INCREMENT PRIMARY KEY,
website VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
self.connection.commit()
except Error as e:
print(f"Error creating table: {e}")
def get_all_passwords(self):
try:
cursor = self.connection.cursor()
cursor.execute("SELECT * FROM passwords")
return cursor.fetchall()
except Error as e:
print(f"Error getting passwords: {e}")
return []
Output:
Conclusion:
Databases are crucial for modern applications as they provide a structured, efficient, and secure
way to handle data. Unlike static file storage, databases offer:
Data Persistence: Information remains safe and organized even after the application closes
Concurrent Access: Multiple users can access and modify data simultaneously
Data Integrity: Built-in rules ensure data remains accurate and consistent
Scalability: Can handle growing amounts of data efficiently
Security: Controlled access with user permissions and encryption
CRUD Operations: Easy to Create, Read, Update, and Delete data
Data Relationships: Connect related information (like users and their passwords)
Query Capabilities: Quickly search and filter large amounts of data
Backup & Recovery: Protect against da