源代码实现基于python+mysql人事管理系统
时间: 2023-12-07 18:05:28 浏览: 80
以下是一个简单的人事管理系统的Python源代码实现,使用MySQL作为数据库存储数据:
首先,需要安装MySQL的Python驱动程序,可以使用pip进行安装:
```
pip install mysql-connector-python
```
接下来,创建一个MySQL数据库,名为"hrms",并创建两个表格"employees"和"departments",分别用于存储员工和部门信息。
employees表格包含以下字段:id, name, age, gender, department_id, salary
departments表格包含以下字段:id, name, description
接着,运行以下代码实现人事管理系统:
```python
import mysql.connector
# 连接MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="hrms"
)
# 定义一个函数,用于添加员工信息
def add_employee(name, age, gender, department_id, salary):
mycursor = mydb.cursor()
sql = "INSERT INTO employees (name, age, gender, department_id, salary) VALUES (%s, %s, %s, %s, %s)"
val = (name, age, gender, department_id, salary)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
# 定义一个函数,用于更新员工信息
def update_employee(id, name, age, gender, department_id, salary):
mycursor = mydb.cursor()
sql = "UPDATE employees SET name = %s, age = %s, gender = %s, department_id = %s, salary = %s WHERE id = %s"
val = (name, age, gender, department_id, salary, id)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) affected.")
# 定义一个函数,用于删除员工信息
def delete_employee(id):
mycursor = mydb.cursor()
sql = "DELETE FROM employees WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted.")
# 定义一个函数,用于查询员工信息
def get_employees():
mycursor = mydb.cursor()
sql = "SELECT employees.id, employees.name, employees.age, employees.gender, departments.name, employees.salary FROM employees INNER JOIN departments ON employees.department_id = departments.id"
mycursor.execute(sql)
result = mycursor.fetchall()
for row in result:
print(row)
# 定义一个函数,用于添加部门信息
def add_department(name, description):
mycursor = mydb.cursor()
sql = "INSERT INTO departments (name, description) VALUES (%s, %s)"
val = (name, description)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
# 定义一个函数,用于更新部门信息
def update_department(id, name, description):
mycursor = mydb.cursor()
sql = "UPDATE departments SET name = %s, description = %s WHERE id = %s"
val = (name, description, id)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) affected.")
# 定义一个函数,用于删除部门信息
def delete_department(id):
mycursor = mydb.cursor()
sql = "DELETE FROM departments WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted.")
# 定义一个函数,用于查询部门信息
def get_departments():
mycursor = mydb.cursor()
sql = "SELECT * FROM departments"
mycursor.execute(sql)
result = mycursor.fetchall()
for row in result:
print(row)
# 添加一些示例数据
add_department("IT", "Information Technology")
add_department("HR", "Human Resources")
add_employee("John Doe", 30, "Male", 1, 5000)
add_employee("Jane Smith", 25, "Female", 1, 6000)
# 查询员工信息
get_employees()
# 更新员工信息
update_employee(1, "John Smith", 31, "Male", 1, 5500)
# 删除员工信息
delete_employee(2)
# 查询部门信息
get_departments()
# 更新部门信息
update_department(1, "IT Department", "Information Technology and Services")
# 删除部门信息
delete_department(2)
# 关闭数据库连接
mydb.close()
```
以上代码实现了一个简单的人事管理系统,可以通过调用各种函数来添加、更新、删除和查询员工和部门信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)