python 员工信息管理系统
import sqlite3
from typing import List
class employeeManagerSystem(object):
def __init__(self):
self.conn = sqlite3.connect('test.db')
self.cursor = self.conn.cursor()
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS "employee" (
"id" integer(10) NOT NULL,
"name" TEXT NOT NULL,
"_gender" TEXT NOT NULL,
"telephone" char(50) NOT NULL,
PRIMARY KEY ("id")
) ''')
def read_all_employee(self) -> List[tuple]:
self.cursor.execute('SELECT * FROM employee')
employee_list = self.cursor.fetchall()
if not employee_list:
print('目前系统中没有存有任何信息!')
print('\n')
return employee_list
print('编号\t\t姓名\t\t性别\t\t手机')
for employee in employee_list:
print("{:^}\t\t{:^}\t\t{:^}\t\t{:^}".format(employee[0],employee[1],employee[2],employee[3]))
print('\n')
return employee_list
def read_employee(self):
id = input('请输入要查询员工的编号: ')
self.cursor.execute(
'SELECT * FROM employee where id=?', (id,))
result = self.cursor.fetchall()
if not result:
print('所查询编号不存在!')
print('\n')
return
print('查询结果如下:')
print('编号\t\t姓名\t\t性别\t\t手机')
print("{:^}\t\t{:^}\t\t{:^}\t\t{:^}".format(result[0][0],result[0][1],result[0][2],result[0][3]))
print('\n')
def add_employee(self):
'''添加员工信息'''
id = input('请输入员工的编号: ')
while True:
if self._is_in_database(id):
print('该编号已存在!')
id = input('请重新输入编号: ')
else:
break
name = input('请输入员工的姓名: ')
_gender = input('请输入性别: ')
telephone = input('请输入员工的电话号码: ')
self.cursor.execute(
'INSERT INTO employee VALUES (?, ?, ?, ?)',
(id, name,_gender, telephone))
self.conn.commit()
print('信息添加成功!')
print('\n')
def update_employee(self):
id = input('请输入要修改信息的员工编号: ')
if not self._is_in_database(id):
print('要修改信息的编号不存在!')
print('\n')
return
name = input('请输入员工的姓名: ')
_gender = input('请输入性别: ')
telephone = input('请输入员工的电话号码: ')
self.cursor.execute(
'''UPDATE employee SET _gender=?,
name=?,
telephone=? WHERE id=?''',
(_gender, name, telephone, id))
self.conn.commit()
print('信息修改成功!')
print('\n')
'''删除员工信息'''
def delete_employee(self):
id = input('请输入要删除信息的员工编号: ')
if not self._is_in_database(id):
print('要删除的编号不存在!')
print('\n')
return
self.cursor.execute(
'DELETE FROM employee WHERE id=?', (id,))
self.conn.commit()
print('信息删除成功!')
print('\n')
def _is_in_database(self, id: str) -> bool:
'''判断员工信息是否在数据库中'''
self.cursor.execute(
'SELECT * FROM employee WHERE id=?', (id,))
result = self.cursor.fetchall()
return True if result else False
def close_connection_database(self) -> None:
'''关闭数据库连接'''
self.conn.close()
if __name__ == '__main__':
employeeManagerSystem = employeeManagerSystem()
while True:
print("======员工信息管理系统V1.0======")
print('1.添加员工信息')
print('2.删除员工信息')
print('3.修改员工信息')
print('4.显示所有员工信息')
print('5.查询指定员工信息')
print('6.退出系统(YES/NO)')
choiceid = input('请输入功能对应的数字: ')
if choiceid == '1':
employeeManagerSystem.add_employee()
elif choiceid == '2':
employeeManagerSystem.delete_employee()
elif choiceid == '3':
employeeManagerSystem.update_employee()
elif choiceid == '4':
employeeManagerSystem.read_all_employee()
elif choiceid == '5':
employeeManagerSystem.read_employee()
elif choiceid == '6':
exits = input("请确认是否要退出(YES/NO)").lower()
if exits=='yes':
employeeManagerSystem.close_connection_database()
print("退出成功")
break
else:
employeeManagerSystem.read_all_employee()