-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.py
372 lines (325 loc) · 14.5 KB
/
program.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# A simple employee database system using Python and MySQL
import pymysql
from time import sleep
from datetime import datetime
import pickle
RED = '\033[91m'
GREEN = '\033[92m'
BLUE = '\033[94m'
RESET = '\033[0m'
banner = \
f"""
{BLUE}
███████ ███ ███ ██████ ██ ██████ ██ ██ ███████ ███████
██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
█████ ██ ████ ██ ██████ ██ ██ ██ ████ █████ █████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ███████ ██████ ██ ███████ ███████
██████ █████ ████████ █████ ██████ █████ ███████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ██ ███████ ██████ ███████ ███████ █████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██ ██ ██ ██████ ██ ██ ███████ ███████
███████ ██ ██ ███████ ████████ ███████ ███ ███
██ ██ ██ ██ ██ ██ ████ ████
███████ ████ ███████ ██ █████ ██ ████ ██
██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ███████ ██ ███████ ██ ██
{RESET}
"""
menu = \
f"""
1. Add a new entry
2. View an entry
3. Update an entry
4. Delete an entry
5. View all entries
6. Delete all entries {RED}(WARNING!){RESET}
7. Export database
8. Export to CSV
9. Exit
"""
def take_input():
user_input = input(f"{GREEN}Enter your choice: \n{RESET}")
# If the user enters a non-integer value or an integer outside 1 to 7, keep asking for input
while not user_input.isdigit() or int(user_input) not in range(1, 10):
print(f"{RED}Invalid input! Please make a correct choice!\n{RESET}")
print(menu)
user_input = input(f"{GREEN}Enter your choice: \n{RESET}")
return int(user_input)
def select_option(option, conn):
match option:
case 1:
add_entry(conn)
case 2:
view_entry(conn)
case 3:
update_entry(conn)
case 4:
delete_entry(conn)
case 5:
view_all_entries(conn)
case 6:
delete_all_entries(conn)
case 7:
export_db(conn)
case 8:
export_csv(conn)
case 9:
exit_prog()
def add_entry(conn):
# Add a new entry to the database
print(f"{GREEN}Enter the following details:{RESET}")
employee_id = input("Employee ID: ")
first_name = input("First name: ")
last_name = input("Last name: ")
dob = input("Date of birth (YYYY-MM-DD): ")
gender = input("Gender: ")
aadhaar_number = input("Aadhaar number: ")
email = input("Email: ")
phone = input("Phone: ")
department = input("Department: ")
designation = input("Designation: ")
start_date = input("Start date (YYYY-MM-DD): ")
end_date = input("End date (YYYY-MM-DD): ")
employment_status = input("Employment status: ")
salary = input("Salary: ")
# Insert the new entry into the database
try:
with conn.cursor() as cursor:
# Add all the values to the database
add_entry_query = """
INSERT INTO Employee (
EmployeeID,
First_name,
Last_name,
DOB,
Gender,
Aadhaar_number,
Email,
Phone,
Department,
Designation,
Start_date,
End_date,
Employment_status,
Salary
)
VALUES (
%s, %s, %s, STR_TO_DATE(%s, '%%Y-%%m-%%d'), %s, %s, %s, %s, %s, %s, STR_TO_DATE(%s, '%%Y-%%m-%%d'), STR_TO_DATE(%s, '%%Y-%%m-%%d'), %s, %s
);
"""
cursor.execute(add_entry_query, (employee_id, first_name, last_name, dob, gender, aadhaar_number, email,
phone, department, designation, start_date, end_date, employment_status,
salary))
conn.commit()
print(f"{GREEN}Entry added successfully!{RESET}")
except pymysql.Error as e:
print(f"{RED}Error adding entry!{RESET}")
print(e)
def view_entry(conn):
# Select the details of an entity based on Employee ID
try:
with conn.cursor() as cursor:
employee_id = input("Enter the Employee ID: ")
query = """
SELECT * FROM Employee WHERE EmployeeID = %s;
"""
cursor.execute(query, (employee_id,))
result = cursor.fetchone()
if result:
print(f"""
{BLUE}Employee ID:{RESET} {result[0]}
{BLUE}First Name:{RESET} {result[1]}
{BLUE}Last Name:{RESET} {result[2]}
{BLUE}DOB:{RESET} {result[3]}
{BLUE}Gender:{RESET} {result[4]}
{BLUE}Aadhaar:{RESET} {result[5]}
{BLUE}Email:{RESET} {result[6]}
{BLUE}Phone:{RESET} {result[7]}
{BLUE}Department:{RESET}{result[8]}
{BLUE}Designation:{RESET} {result[9]}
{BLUE}Start Date:{RESET} {result[10]}
{BLUE}End Date:{RESET} {result[11]}
{BLUE}Employment Status:{RESET} {result[12]}
{BLUE}Salary:{RESET} {result[13]}
""")
else:
print(f"{RED} Employee not found! {RESET}")
except pymysql.Error as e:
print(f"{RED}Error adding entry!{RESET}")
print(e)
def update_entry(conn):
try:
with conn.cursor() as cursor:
employee_id = input("Enter the Employee ID: ")
first_name = input("Enter the first name: ")
last_name = input("Enter the last name: ")
dob = input("Enter the date of birth (YYYY-MM-DD): ")
gender = input("Enter gender: ")
aadhaar_number = input("Enter Aadhaar number: ")
email = input("Enter email: ")
phone = input("Enter phone number: ")
department = input("Enter department: ")
designation = input("Enter designation: ")
start_date = input("Enter start date (YYYY-MM-DD): ")
end_date = input("Enter end date (YYYY-MM-DD): ")
employment_status = input("Enter employment status: ")
salary = input("Enter salary: ")
query = """
UPDATE Employee SET First_name = %s, Last_name = %s, dob = STR_TO_DATE(%s, '%%Y-%%m-%%d'), gender = %s, aadhaar_number = %s, email = %s, phone = %s, department = %s, designation = %s, start_date = STR_TO_DATE(%s, '%%Y-%%m-%%d'), end_date = STR_TO_DATE(%s, '%%Y-%%m-%%d'), employment_status = %s, salary = %s WHERE EmployeeID = %s;
"""
cursor.execute(query, (first_name, last_name, dob, gender, aadhaar_number, email, phone, department, designation, start_date, end_date, employment_status, salary, employee_id))
conn.commit()
print(f"{GREEN}Entry updated successfully!{RESET}")
except pymysql.Error as e:
print(f"{RED}Error updating entry!{RESET}")
print(e)
def delete_entry(conn):
try:
with conn.cursor() as cursor:
employee_id = input("Enter the Employee ID: ")
query = """
DELETE FROM Employee WHERE EmployeeID = %s;
"""
cursor.execute(query, (employee_id,))
conn.commit()
print(f"{GREEN}Entry deleted successfully!{RESET}")
except pymysql.Error as e:
print(f"{RED}Error deleting entry!{RESET}")
print(e)
def view_all_entries(conn):
try:
with conn.cursor() as cursor:
# SE:ECT * FROM Employee;
query = """
SELECT * FROM Employee;
"""
cursor.execute(query)
result = cursor.fetchall()
if result:
print(f"{BLUE}Employee ID\tFirst Name\tLast Name\tDOB\tGender\tAadhaar\tEmail\tPhone\tDepartment\tDesignation\tStart Date\tEnd Date\tEmployment Status\tSalary{RESET}")
for row in result:
print(f"{row[0]}\t{row[1]}\t{row[2]}\t{row[3]}\t{row[4]}\t{row[5]}\t{row[6]}\t{row[7]}\t{row[8]}\t{row[9]}\t{row[10]}\t{row[11]}\t{row[12]}\t{row[13]}")
else:
print(f"{RED}No entries found!{RESET}")
except pymysql.Error as e:
print(f"{RED}Error viewing entries!{RESET}")
print(e)
def delete_all_entries(conn):
choice = input(f"{RED}Are you sure you want to delete all entries? (y/n): {RESET}")
if choice.lower() == 'y':
try:
with conn.cursor() as cursor:
query = """
DELETE FROM Employee;
"""
cursor.execute(query)
conn.commit()
print(f"{GREEN}All entries deleted successfully!{RESET}")
except pymysql.Error as e:
print(f"{RED}Error deleting entries!{RESET}")
print(e)
else:
pass
def export_db(conn):
try:
with conn.cursor() as cursor:
query = """
SELECT * FROM Employee;
"""
cursor.execute(query)
result = cursor.fetchall()
if result:
with open(f"employee {datetime.now()}.pickle", "wb") as f:
pickle.dump(result, f)
print(f"{GREEN}Database exported successfully!{RESET}")
else:
print(f"{RED}No entries found!{RESET}")
except pymysql.Error as e:
print(f"{RED}Error exporting database!{RESET}")
print(e)
def export_csv(conn):
try:
with conn.cursor() as cursor:
query = """
SELECT * FROM Employee;
"""
cursor.execute(query)
result = cursor.fetchall()
if result:
with open(f"employee {datetime.now()}.csv", "w") as f:
f.write("Employee ID,First Name,Last Name,DOB,Gender,Aadhaar,Email,Phone,Department,Designation,Start Date,End Date,Employment Status,Salary\n")
for row in result:
f.write(f"{row[0]},{row[1]},{row[2]},{row[3]},{row[4]},{row[5]},{row[6]},{row[7]},{row[8]},{row[9]},{row[10]},{row[11]},{row[12]},{row[13]}\n")
print(f"{GREEN}Database exported successfully!{RESET}")
else:
print(f"{RED}No entries found!{RESET}")
except pymysql.Error as e:
print(f"{RED}Error exporting database!{RESET}")
print(e)
def exit_prog():
print(f"{GREEN}Thank you for using the program!{RESET}")
exit()
def check_db():
db_config = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'rootpass',
'charset': 'utf8mb4',
}
try:
init_conn = pymysql.connect(**db_config)
with init_conn.cursor() as cursor:
create_db_query = "CREATE DATABASE IF NOT EXISTS employee_db;"
cursor.execute(create_db_query)
init_conn.commit()
init_conn.select_db('employee_db')
# Create a new table with three numeric columns
create_table_query = """
CREATE TABLE IF NOT EXISTS Employee (
EmployeeID INT PRIMARY KEY,
First_name VARCHAR(50),
Last_name VARCHAR(50),
DOB DATE,
Gender VARCHAR(10),
Aadhaar_number INT,
Email VARCHAR(100),
Phone VARCHAR(10),
Department VARCHAR(50),
Designation VARCHAR(50),
Start_date DATE,
End_date DATE,
Employment_status VARCHAR(20),
Salary DECIMAL(10, 2)
);
"""
cursor.execute(create_table_query)
init_conn.commit()
except pymysql.err.OperationalError:
print(
f"{RED}Error connecting to database!{RESET}\n\
Try running the following command and try again:\n\n\
sudo docker-compose up --build -d")
exit()
def db_connect():
db_config = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'rootpass',
'db': 'employee_db',
'charset': 'utf8mb4',
}
return pymysql.connect(**db_config)
if __name__ == '__main__':
check_db()
conn = db_connect()
print(banner)
sleep(1)
while True:
print(menu)
a = take_input()
select_option(a, conn)
sleep(1)