-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython code
127 lines (108 loc) · 3.6 KB
/
python code
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import mysql.connector
# Database connection
con = mysql.connector.connect(
host="localhost",
user="root",
password="*****",
database="emp"
)
cursor = con.cursor()
# Function to check if an employee exists
def check_employee(employee_id):
sql = 'SELECT * FROM employees WHERE id=%s'
cursor.execute(sql, (employee_id,))
result = cursor.fetchall() # Fetch all results to clear the result set
return len(result) == 1
# Function to add an employee
def add_employee():
Id = input("Enter Employee Id: ")
if check_employee(Id):
print("Employee already exists. Please try again.")
return
Name = input("Enter Employee Name: ")
Post = input("Enter Employee Post: ")
Salary = input("Enter Employee Salary: ")
sql = 'INSERT INTO employees (id, name, post, salary) VALUES (%s, %s, %s, %s)'
data = (Id, Name, Post, Salary)
try:
cursor.execute(sql, data)
con.commit()
print("Employee Added Successfully")
except mysql.connector.Error as err:
print(f"Error: {err}")
con.rollback()
# Function to remove an employee
def remove_employee():
Id = input("Enter Employee Id: ")
if not check_employee(Id):
print("Employee does not exist. Please try again.")
return
sql = 'DELETE FROM employees WHERE id=%s'
data = (Id,)
try:
cursor.execute(sql, data)
con.commit()
print("Employee Removed Successfully")
except mysql.connector.Error as err:
print(f"Error: {err}")
con.rollback()
# Function to promote an employee
def promote_employee():
Id = input("Enter Employee's Id: ")
if not check_employee(Id):
print("Employee does not exist. Please try again.")
return
try:
Amount = int(input("Enter increase in Salary: "))
sql_select = 'SELECT salary FROM employees WHERE id=%s'
cursor.execute(sql_select, (Id,))
current_salary = cursor.fetchone()[0]
new_salary = current_salary + Amount
sql_update = 'UPDATE employees SET salary=%s WHERE id=%s'
cursor.execute(sql_update, (new_salary, Id))
con.commit()
print("Employee Promoted Successfully")
except (ValueError, mysql.connector.Error) as e:
print(f"Error: {e}")
con.rollback()
# Function to display all employees
def display_employees():
try:
sql = 'SELECT * FROM employees'
cursor.execute(sql)
employees = cursor.fetchall()
for employee in employees:
print("Employee Id : ", employee[0])
print("Employee Name : ", employee[1])
print("Employee Post : ", employee[2])
print("Employee Salary : ", employee[3])
print("------------------------------------")
except mysql.connector.Error as err:
print(f"Error: {err}")
# Function to display the menu
def menu():
while True:
print("\nWelcome to Employee Management Record")
print("Press:")
print("1.Add Employee")
print("2.Remove Employee")
print("3.Promote Employee")
print("4.Display Employees")
print("5.Exit")
ch = input("Enter your Choice: ")
if ch == '1':
add_employee()
elif ch == '2':
remove_employee()
elif ch == '3':
promote_employee()
elif ch == '4':
print("\n")
display_employees()
elif ch == '5':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid Choice! Please try again.")
if __name__ == "__main__":
menu()