📜  使用Python的员工管理系统

📅  最后修改于: 2022-05-13 01:55:21.324000             🧑  作者: Mango

使用Python的员工管理系统

任务是用Python创建一个数据库驱动的员工管理系统,它将信息存储在 MySQL 数据库中。该脚本将包含以下操作:

  • 添加员工
  • 删除员工
  • 提拔员工
  • 显示员工

这个想法是我们通过使用不同的函数在我们的员工记录中执行不同的更改,例如 Add_Employee 将在我们的 Employee 中插入一个新行,此外,我们将创建一个 Remove Employee函数,它将删除我们中任何特定现有员工的记录。员工表。该系统的工作原理是从数据库中获取信息,对获取的数据进行所需的更改,并将更改应用到我们将在我们的提升员工系统中看到的记录中。我们还可以使用 Display Employee函数所有现有员工的信息。将我们的程序连接到数据库的主要优点是即使在多次关闭我们的程序后信息也不会丢失。

入门

为了在Python中创建使用 MySQL 数据库的员工管理系统,我们需要将Python与 MySQL 连接起来。

为了建立连接,我们需要安装mysqlconnector ,这可以通过在 Windows 的命令提示符中编写以下命令来完成。

pip install mysqlconnector

现在成功安装 mysqlconnector 后,我们可以使用Python连接 MySQL,这可以通过编写以下代码来完成



Python3
import mysql.connector
 
 
con = mysql.connector.connect(
    host="localhost", user="root", password="password", database="emp")


Python3
# Function To Check if Employee with
# given Id Exist or Not
 
def check_employee(employee_id):
 
    # Query to select all Rows f
    # rom employee Table
    sql = 'select * from empd where id=%s'
 
    # making cursor buffered to make
    # rowcount method work properly
    c = con.cursor(buffered=True)
    data = (employee_id,)
 
    # Executing the SQL Query
    c.execute(sql, data)
 
    # rowcount method to find
    # number of rows with given values
    r = c.rowcount
     
    if r == 1:
        return True
    else:
        return False


Python3
# Function to mAdd_Employee
 
def Add_Employ():
 
    Id = input("Enter Employ Id : ")
 
    # Checking if Employee with given Id
    # Already Exist or Not
    if(check_employee(Id) == True):
        print("Employee aready exists\nTry Again\n")
        menu()
     
    else:
        Name = input("Enter Employ Name : ")
        Post = input("Enter Employ Post : ")
        Salary = input("Enter Employ Salary : ")
        data = (Id, Name, Post, Salary)
 
        # Inserting Employee details in the Employee
        # Table
        sql = 'insert into empd values(%s,%s,%s,%s)'
        c = con.cursor()
 
        # Executing the SQL Query
        c.execute(sql, data)
 
        # commit() method to make changes in the table
        con.commit()
        print("Employ Added Successfully ")
        menu()


Python3
# Function to Remove Employee with given Id
def Remove_Employ():
    Id = input("Enter Employ Id : ")
 
    # Checking if Employee with given Id
    # Exist or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
     
    else:
         
        # Query to Delete Employye from Table
        sql = 'delete from empd where id=%s'
        data = (Id,)
        c = con.cursor()
 
        # Executing the SQL Query
        c.execute(sql, data)
 
        # commit() method to make changes in
        # the table
        con.commit()
        print("Employee Removed")
        menu()


Python3
# Function to Promote Employee
def Promote_Employee():
    Id = int(input("Enter Employ's Id"))
 
    # Checking if Employee with given Id
    # Exist or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
    else:
        Amount = int(input("Enter increase in Salary"))
 
        # Query to Fetch Salary of Employee with
        # given Id
        sql = 'select salary from empd where id=%s'
        data = (Id,)
        c = con.cursor()
 
        # Executing the SQL Query
        c.execute(sql, data)
 
        # Fetching Salary of Employee with given Id
        r = c.fetchone()
        t = r[0]+Amount
 
        # Query to Update Salary of Employee with
        # given Id
        sql = 'update empd set salary=%s where id=%s'
        d = (t, Id)
 
        # Executing the SQL Query
        c.execute(sql, d)
 
        # commit() method to make changes in the table
        con.commit()
        print("Employe Promoted")
        menu()


Python3
# Function to Display All Employees
# from Employee Table
 
def Display_Employees():
     
    # query to select all rows from
    # Employee Table
    sql = 'select * from empd'
    c = con.cursor()
     
    # Executing the SQL Query
    c.execute(sql)
     
    # Fetching all details of all the
    # Employees
    r = c.fetchall()
    for i in r:
        print("Employ Id : ", i[0])
        print("Employ Name : ", i[1])
        print("Employ Post : ", i[2])
        print("Employ Salary : ", i[3])
        print("-----------------------------\
        -------------------------------------\
        -----------------------------------")
    menu()


Python3
# menu function to display the menu
def menu():
    print("Welcome to Employ Management Record")
    print("Press ")
    print("1 to Add Employ")
    print("2 to Remove Employ ")
    print("3 to Promote Employ")
    print("4 to Display Employees")
    print("5 to Exit")
     
    # Taking choice from user
    ch = int(input("Enter your Choice "))
    if ch == 1:
        Add_Employ()
         
    elif ch == 2:
        Remove_Employ()
         
    elif ch == 3:
        Promote_Employee()
         
    elif ch == 4:
        Display_Employees()
         
    elif ch == 5:
        exit(0)
         
    else:
        print("Invalid Choice")
        menu()


Python3
# importing mysql connector
import mysql.connector
 
# making Connection
con = mysql.connector.connect(
    host="localhost", user="root", password="password", database="emp")
 
# Function to mAdd_Employee
def Add_Employ():
 
    Id = input("Enter Employ Id : ")
     
    # Checking if Employee with given Id
    # Already Exist or Not
    if(check_employee(Id) == True):
        print("Employee aready exists\nTry Again\n")
        menu()
         
    else:
        Name = input("Enter Employ Name : ")
        Post = input("Enter Employ Post : ")
        Salary = input("Enter Employ Salary : ")
        data = (Id, Name, Post, Salary)
     
        # Inserting Employee details in
        # the Employee Table
        sql = 'insert into empd values(%s,%s,%s,%s)'
        c = con.cursor()
         
        # Executing the SQL Query
        c.execute(sql, data)
         
        # commit() method to make changes in
        # the table
        con.commit()
        print("Employ Added Successfully ")
        menu()
 
# Function to Promote Employee
def Promote_Employee():
    Id = int(input("Enter Employ's Id"))
     
    # Checking if Employee with given Id
    # Exist or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
    else:
        Amount = int(input("Enter increase in Salary"))
         
        # Query to Fetch Salary of Employee
        # with given Id
        sql = 'select salary from empd where id=%s'
        data = (Id,)
        c = con.cursor()
         
        # Executing the SQL Query
        c.execute(sql, data)
         
        # Fetching Salary of Employee with given Id
        r = c.fetchone()
        t = r[0]+Amount
         
        # Query to Update Salary of Employee with
        # given Id
        sql = 'update empd set salary=%s where id=%s'
        d = (t, Id)
         
        # Executing the SQL Query
        c.execute(sql, d)
         
        # commit() method to make changes in the table
        con.commit()
        print("Employe Promoted")
        menu()
 
# Function to Remove Employee with given Id
def Remove_Employ():
    Id = input("Enter Employ Id : ")
     
    # Checking if Employee with given Id Exist
    # or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
    else:
         
        # Query to Delete Employye from Table
        sql = 'delete from empd where id=%s'
        data = (Id,)
        c = con.cursor()
         
        # Executing the SQL Query
        c.execute(sql, data)
         
        # commit() method to make changes in
        # the table
        con.commit()
        print("Employee Removed")
        menu()
 
 
# Function To Check if Employee with
# given Id Exist or Not
def check_employee(employee_id):
     
    # Query to select all Rows f
    # rom employee Table
    sql = 'select * from empd where id=%s'
     
    # making cursor buffered to make
    # rowcount method work properly
    c = con.cursor(buffered=True)
    data = (employee_id,)
     
    # Executing the SQL Query
    c.execute(sql, data)
     
    # rowcount method to find
    # number of rows with given values
    r = c.rowcount
    if r == 1:
        return True
    else:
        return False
 
# Function to Display All Employees
# from Employee Table
def Display_Employees():
     
    # query to select all rows from
    # Employee Table
    sql = 'select * from empd'
    c = con.cursor()
     
    # Executing the SQL Query
    c.execute(sql)
     
    # Fetching all details of all the
    # Employees
    r = c.fetchall()
    for i in r:
        print("Employ Id : ", i[0])
        print("Employ Name : ", i[1])
        print("Employ Post : ", i[2])
        print("Employ Salary : ", i[3])
        print("---------------------\
        -----------------------------\
        ------------------------------\
        ---------------------")
         
    menu()
 
# menu function to display menu
def menu():
    print("Welcome to Employ Management Record")
    print("Press ")
    print("1 to Add Employ")
    print("2 to Remove Employ ")
    print("3 to Promote Employ")
    print("4 to Display Employees")
    print("5 to Exit")
 
    ch = int(input("Enter your Choice "))
    if ch == 1:
        Add_Employ()
    elif ch == 2:
        Remove_Employ()
    elif ch == 3:
        Promote_Employee()
    elif ch == 4:
        Display_Employees()
    elif ch == 5:
        exit(0)
    else:
        print("Invalid Choice")
        menu()
 
 
# Calling menu function
menu()


现在我们完成了连接,所以我们可以专注于我们的员工管理系统

使用表:

员工记录

这个想法是我们保留所有关于 上表中的员工并在需要时操作该表。所以现在我们将详细了解每个操作的工作情况。

检查员工函数

检查员工函数以员工 id 作为参数,并检查员工详细信息记录中是否存在具有给定 id 的任何员工。为了检查这一点,它使用cursor.rowcount()函数计算与给定详细信息匹配的行数。它是一个实用函数,我们将在后面的操作中看到它的使用,例如添加员工函数等。

程序:

蟒蛇3

# Function To Check if Employee with
# given Id Exist or Not
 
def check_employee(employee_id):
 
    # Query to select all Rows f
    # rom employee Table
    sql = 'select * from empd where id=%s'
 
    # making cursor buffered to make
    # rowcount method work properly
    c = con.cursor(buffered=True)
    data = (employee_id,)
 
    # Executing the SQL Query
    c.execute(sql, data)
 
    # rowcount method to find
    # number of rows with given values
    r = c.rowcount
     
    if r == 1:
        return True
    else:
        return False

添加员工函数

添加员工函数将要求提供员工 ID 并使用检查员工函数检查要添加的员工是否已存在于我们的记录中,如果员工详细信息尚不存在,则它会要求添加员工的详细信息,例如员工姓名、员工职位和员工工资。现在,在从该系统的用户那里获取所有此类详细信息后,它只需将信息插入到我们的员工详细信息表中即可。



程序:

蟒蛇3

# Function to mAdd_Employee
 
def Add_Employ():
 
    Id = input("Enter Employ Id : ")
 
    # Checking if Employee with given Id
    # Already Exist or Not
    if(check_employee(Id) == True):
        print("Employee aready exists\nTry Again\n")
        menu()
     
    else:
        Name = input("Enter Employ Name : ")
        Post = input("Enter Employ Post : ")
        Salary = input("Enter Employ Salary : ")
        data = (Id, Name, Post, Salary)
 
        # Inserting Employee details in the Employee
        # Table
        sql = 'insert into empd values(%s,%s,%s,%s)'
        c = con.cursor()
 
        # Executing the SQL Query
        c.execute(sql, data)
 
        # commit() method to make changes in the table
        con.commit()
        print("Employ Added Successfully ")
        menu()

删除员工函数

删除员工函数将简单地要求删除员工的 ID,因为 ID 是我们员工详细信息记录中的主键,因为可以有两个同名的员工,但他们必须有一个唯一的 ID。删除员工函数使用检查员工函数来检查要删除的员工是否存在于我们的记录中,如果员工详细信息存在,然后在获取有效员工 ID 后删除与该员工 ID 对应的记录。

程序

蟒蛇3

# Function to Remove Employee with given Id
def Remove_Employ():
    Id = input("Enter Employ Id : ")
 
    # Checking if Employee with given Id
    # Exist or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
     
    else:
         
        # Query to Delete Employye from Table
        sql = 'delete from empd where id=%s'
        data = (Id,)
        c = con.cursor()
 
        # Executing the SQL Query
        c.execute(sql, data)
 
        # commit() method to make changes in
        # the table
        con.commit()
        print("Employee Removed")
        menu()

提升员工函数

晋升员工函数将询问员工 ID 并使用检查员工函数检查要晋升的员工是否存在于我们的记录中,如果员工详细信息存在,则它将询问要增加他的工资的金额。获得有效的详细信息后,它会按给定的金额增加给定 id 的员工的工资。

程序

蟒蛇3

# Function to Promote Employee
def Promote_Employee():
    Id = int(input("Enter Employ's Id"))
 
    # Checking if Employee with given Id
    # Exist or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
    else:
        Amount = int(input("Enter increase in Salary"))
 
        # Query to Fetch Salary of Employee with
        # given Id
        sql = 'select salary from empd where id=%s'
        data = (Id,)
        c = con.cursor()
 
        # Executing the SQL Query
        c.execute(sql, data)
 
        # Fetching Salary of Employee with given Id
        r = c.fetchone()
        t = r[0]+Amount
 
        # Query to Update Salary of Employee with
        # given Id
        sql = 'update empd set salary=%s where id=%s'
        d = (t, Id)
 
        # Executing the SQL Query
        c.execute(sql, d)
 
        # commit() method to make changes in the table
        con.commit()
        print("Employe Promoted")
        menu()

显示员工函数

显示员工函数只是一个 SQL 选择查询,它获取存储在员工详细信息表中的所有记录并逐行打印它们。

程序:



蟒蛇3

# Function to Display All Employees
# from Employee Table
 
def Display_Employees():
     
    # query to select all rows from
    # Employee Table
    sql = 'select * from empd'
    c = con.cursor()
     
    # Executing the SQL Query
    c.execute(sql)
     
    # Fetching all details of all the
    # Employees
    r = c.fetchall()
    for i in r:
        print("Employ Id : ", i[0])
        print("Employ Name : ", i[1])
        print("Employ Post : ", i[2])
        print("Employ Salary : ", i[3])
        print("-----------------------------\
        -------------------------------------\
        -----------------------------------")
    menu()


菜单函数

菜单函数向用户显示菜单,并要求用户输入他的选择以执行添加员工、删除员工等操作。

程序

蟒蛇3

# menu function to display the menu
def menu():
    print("Welcome to Employ Management Record")
    print("Press ")
    print("1 to Add Employ")
    print("2 to Remove Employ ")
    print("3 to Promote Employ")
    print("4 to Display Employees")
    print("5 to Exit")
     
    # Taking choice from user
    ch = int(input("Enter your Choice "))
    if ch == 1:
        Add_Employ()
         
    elif ch == 2:
        Remove_Employ()
         
    elif ch == 3:
        Promote_Employee()
         
    elif ch == 4:
        Display_Employees()
         
    elif ch == 5:
        exit(0)
         
    else:
        print("Invalid Choice")
        menu()

完整代码:

蟒蛇3

# importing mysql connector
import mysql.connector
 
# making Connection
con = mysql.connector.connect(
    host="localhost", user="root", password="password", database="emp")
 
# Function to mAdd_Employee
def Add_Employ():
 
    Id = input("Enter Employ Id : ")
     
    # Checking if Employee with given Id
    # Already Exist or Not
    if(check_employee(Id) == True):
        print("Employee aready exists\nTry Again\n")
        menu()
         
    else:
        Name = input("Enter Employ Name : ")
        Post = input("Enter Employ Post : ")
        Salary = input("Enter Employ Salary : ")
        data = (Id, Name, Post, Salary)
     
        # Inserting Employee details in
        # the Employee Table
        sql = 'insert into empd values(%s,%s,%s,%s)'
        c = con.cursor()
         
        # Executing the SQL Query
        c.execute(sql, data)
         
        # commit() method to make changes in
        # the table
        con.commit()
        print("Employ Added Successfully ")
        menu()
 
# Function to Promote Employee
def Promote_Employee():
    Id = int(input("Enter Employ's Id"))
     
    # Checking if Employee with given Id
    # Exist or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
    else:
        Amount = int(input("Enter increase in Salary"))
         
        # Query to Fetch Salary of Employee
        # with given Id
        sql = 'select salary from empd where id=%s'
        data = (Id,)
        c = con.cursor()
         
        # Executing the SQL Query
        c.execute(sql, data)
         
        # Fetching Salary of Employee with given Id
        r = c.fetchone()
        t = r[0]+Amount
         
        # Query to Update Salary of Employee with
        # given Id
        sql = 'update empd set salary=%s where id=%s'
        d = (t, Id)
         
        # Executing the SQL Query
        c.execute(sql, d)
         
        # commit() method to make changes in the table
        con.commit()
        print("Employe Promoted")
        menu()
 
# Function to Remove Employee with given Id
def Remove_Employ():
    Id = input("Enter Employ Id : ")
     
    # Checking if Employee with given Id Exist
    # or Not
    if(check_employee(Id) == False):
        print("Employee does not  exists\nTry Again\n")
        menu()
    else:
         
        # Query to Delete Employye from Table
        sql = 'delete from empd where id=%s'
        data = (Id,)
        c = con.cursor()
         
        # Executing the SQL Query
        c.execute(sql, data)
         
        # commit() method to make changes in
        # the table
        con.commit()
        print("Employee Removed")
        menu()
 
 
# Function To Check if Employee with
# given Id Exist or Not
def check_employee(employee_id):
     
    # Query to select all Rows f
    # rom employee Table
    sql = 'select * from empd where id=%s'
     
    # making cursor buffered to make
    # rowcount method work properly
    c = con.cursor(buffered=True)
    data = (employee_id,)
     
    # Executing the SQL Query
    c.execute(sql, data)
     
    # rowcount method to find
    # number of rows with given values
    r = c.rowcount
    if r == 1:
        return True
    else:
        return False
 
# Function to Display All Employees
# from Employee Table
def Display_Employees():
     
    # query to select all rows from
    # Employee Table
    sql = 'select * from empd'
    c = con.cursor()
     
    # Executing the SQL Query
    c.execute(sql)
     
    # Fetching all details of all the
    # Employees
    r = c.fetchall()
    for i in r:
        print("Employ Id : ", i[0])
        print("Employ Name : ", i[1])
        print("Employ Post : ", i[2])
        print("Employ Salary : ", i[3])
        print("---------------------\
        -----------------------------\
        ------------------------------\
        ---------------------")
         
    menu()
 
# menu function to display menu
def menu():
    print("Welcome to Employ Management Record")
    print("Press ")
    print("1 to Add Employ")
    print("2 to Remove Employ ")
    print("3 to Promote Employ")
    print("4 to Display Employees")
    print("5 to Exit")
 
    ch = int(input("Enter your Choice "))
    if ch == 1:
        Add_Employ()
    elif ch == 2:
        Remove_Employ()
    elif ch == 3:
        Promote_Employee()
    elif ch == 4:
        Display_Employees()
    elif ch == 5:
        exit(0)
    else:
        print("Invalid Choice")
        menu()
 
 
# Calling menu function
menu()

输出