-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUD.py
More file actions
95 lines (79 loc) · 2.86 KB
/
Copy pathCRUD.py
File metadata and controls
95 lines (79 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import sqlite3
# Create a connection to the SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('employees.db')
cursor = conn.cursor()
# Create an Employee table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
position TEXT NOT NULL,
salary REAL NOT NULL
)
''')
# Function to add an employee
def add_employee(name, position, salary):
cursor.execute('''
INSERT INTO employees (name, position, salary)
VALUES (?, ?, ?)
''', (name, position, salary))
conn.commit()
print(f"Employee {name} added successfully!")
# Function to view all employees
def view_employees():
cursor.execute('SELECT * FROM employees')
employees = cursor.fetchall()
if employees:
for employee in employees:
print(f"ID: {employee[0]}, Name: {employee[1]}, Position: {employee[2]}, Salary: {employee[3]}")
else:
print("No employees found.")
# Function to update an employee's details
def update_employee(employee_id, name, position, salary):
cursor.execute('''
UPDATE employees
SET name = ?, position = ?, salary = ?
WHERE id = ?
''', (name, position, salary, employee_id))
conn.commit()
print(f"Employee ID {employee_id} updated successfully!")
# Function to delete an employee
def delete_employee(employee_id):
cursor.execute('DELETE FROM employees WHERE id = ?', (employee_id,))
conn.commit()
print(f"Employee ID {employee_id} deleted successfully!")
# Main Menu
def menu():
while True:
print("\n--- Employee Management System ---")
print("1. Add Employee")
print("2. View Employees")
print("3. Update Employee")
print("4. Delete Employee")
print("5. Exit")
choice = input("Choose an option: ")
if choice == '1':
name = input("Enter name: ")
position = input("Enter position: ")
salary = float(input("Enter salary: "))
add_employee(name, position, salary)
elif choice == '2':
view_employees()
elif choice == '3':
employee_id = int(input("Enter employee ID to update: "))
name = input("Enter new name: ")
position = input("Enter new position: ")
salary = float(input("Enter new salary: "))
update_employee(employee_id, name, position, salary)
elif choice == '4':
employee_id = int(input("Enter employee ID to delete: "))
delete_employee(employee_id)
elif choice == '5':
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")
# Run the menu
menu()
# Close the database connection when done
conn.close()