python 员工信息管理系统

python 员工信息管理系统

# -*- coding: utf-8 -*-
import sqlite3
from typing import List

class employeeManagerSystem(object):
   
    def __init__(self):
       # 连接数据库 test.db, 如果不存在就创建
        self.conn = sqlite3.connect('test.db')
        # 定义操作数据库的 cursor()
        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()
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_44322234

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值