-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_handler.py
More file actions
70 lines (62 loc) · 2.32 KB
/
db_handler.py
File metadata and controls
70 lines (62 loc) · 2.32 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
import sqlite3
def create_table():
conn = sqlite3.connect('Employees.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Employees(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(70),
role TEXT,
salary FLOAT,
department TEXT,
hire_date DATE,
sex VARCHAR(6) CHECK(sex IN('Male','Female')),
status TEXT)
''')
conn.commit()
conn.close()
def get_all_employees():
conn = sqlite3.connect('Employees.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM Employees')
employees = cursor.fetchall()
conn.close()
return employees
def add_employee(name, role, salary, department, hire_date, sex, status):
conn = sqlite3.connect('Employees.db')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO Employees(name, role, salary, department, hire_date, sex, status)
VALUES (?,?,?,?,?,?,?)
''', (name, role, salary, department, hire_date, sex, status))
conn.commit()
conn.close()
def remove_employee(id):
conn = sqlite3.connect('Employees.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM Employees WHERE id = ?', (id,))
conn.commit()
conn.close()
def delete_all_employees():
conn = sqlite3.connect('Employees.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM Employees')
conn.commit()
conn.close()
def update_employee_record(id, new_name, new_role, new_salary, new_department, new_hire_date, new_sex, new_status):
conn = sqlite3.connect('Employees.db')
cursor = conn.cursor()
cursor.execute('''
UPDATE Employees SET name = ?, role = ?, salary = ?, department = ?, hire_date = ?, sex = ?, status = ?
WHERE id = ?
''', (new_name, new_role, new_salary, new_department, new_hire_date, new_sex, new_status, id))
conn.commit()
conn.close()
def is_employee_exists(id):
conn = sqlite3.connect('Employees.db')
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM Employees WHERE id = ?', (id,))
result = cursor.fetchone()
conn.close()
return result[0] > 0
create_table()