用Python编写一个“员工信息管理系统”实现对员工信息的管理,数据库中有3张表:员工信息表tbemp、专业表tb_profession、部门表t_dept
时间: 2024-06-08 14:12:10 浏览: 178
基于Python的员工信息管理系统
5星 · 资源好评率100%
好的,我可以帮你实现这个功能。首先,我们需要安装MySQL数据库,并创建一个名为 `employee_management` 的数据库。然后创建三个表 `tbemp`, `tb_profession` 和 `t_dept`:
```mysql
CREATE TABLE `tbemp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` int(11) NOT NULL,
`gender` varchar(10) NOT NULL,
`phone` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(100) NOT NULL,
`profession_id` int(11) NOT NULL,
`dept_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `profession_id` (`profession_id`),
KEY `dept_id` (`dept_id`),
CONSTRAINT `tbemp_ibfk_1` FOREIGN KEY (`profession_id`) REFERENCES `tb_profession` (`id`),
CONSTRAINT `tbemp_ibfk_2` FOREIGN KEY (`dept_id`) REFERENCES `t_dept` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `tb_profession` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `t_dept` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
然后,我们可以使用 Python 和 MySQL 进行连接和操作。这里我们使用 `mysql-connector-python` 库来操作MySQL数据库。先安装这个库:
```bash
pip install mysql-connector-python
```
然后编写Python代码实现员工信息管理系统:
```python
import mysql.connector
# 连接数据库
cnx = mysql.connector.connect(user='root', password='123456', host='localhost', database='employee_management')
cursor = cnx.cursor()
# 查询所有员工信息
def query_all_employees():
query = "SELECT tbemp.id, tbemp.name, tbemp.age, tbemp.gender, tbemp.phone, tbemp.email, tbemp.address, tb_profession.name, t_dept.name FROM tbemp INNER JOIN tb_profession ON tbemp.profession_id=tb_profession.id INNER JOIN t_dept ON tbemp.dept_id=t_dept.id"
cursor.execute(query)
for row in cursor.fetchall():
print(row)
# 添加员工信息
def add_employee(name, age, gender, phone, email, address, profession_id, dept_id):
insert = "INSERT INTO tbemp (name, age, gender, phone, email, address, profession_id, dept_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
val = (name, age, gender, phone, email, address, profession_id, dept_id)
cursor.execute(insert, val)
cnx.commit()
# 修改员工信息
def update_employee(id, name, age, gender, phone, email, address, profession_id, dept_id):
update = "UPDATE tbemp SET name=%s, age=%s, gender=%s, phone=%s, email=%s, address=%s, profession_id=%s, dept_id=%s WHERE id=%s"
val = (name, age, gender, phone, email, address, profession_id, dept_id, id)
cursor.execute(update, val)
cnx.commit()
# 删除员工信息
def delete_employee(id):
delete = "DELETE FROM tbemp WHERE id=%s"
val = (id,)
cursor.execute(delete, val)
cnx.commit()
# 查询所有专业信息
def query_all_professions():
query = "SELECT * FROM tb_profession"
cursor.execute(query)
for row in cursor.fetchall():
print(row)
# 添加专业信息
def add_profession(name):
insert = "INSERT INTO tb_profession (name) VALUES (%s)"
val = (name,)
cursor.execute(insert, val)
cnx.commit()
# 修改专业信息
def update_profession(id, name):
update = "UPDATE tb_profession SET name=%s WHERE id=%s"
val = (name, id)
cursor.execute(update, val)
cnx.commit()
# 删除专业信息
def delete_profession(id):
delete = "DELETE FROM tb_profession WHERE id=%s"
val = (id,)
cursor.execute(delete, val)
cnx.commit()
# 查询所有部门信息
def query_all_depts():
query = "SELECT * FROM t_dept"
cursor.execute(query)
for row in cursor.fetchall():
print(row)
# 添加部门信息
def add_dept(name):
insert = "INSERT INTO t_dept (name) VALUES (%s)"
val = (name,)
cursor.execute(insert, val)
cnx.commit()
# 修改部门信息
def update_dept(id, name):
update = "UPDATE t_dept SET name=%s WHERE id=%s"
val = (name, id)
cursor.execute(update, val)
cnx.commit()
# 删除部门信息
def delete_dept(id):
delete = "DELETE FROM t_dept WHERE id=%s"
val = (id,)
cursor.execute(delete, val)
cnx.commit()
# 关闭数据库连接
cursor.close()
cnx.close()
```
现在,我们就可以通过调用这些函数来操作员工信息、专业信息和部门信息了。比如:
```python
# 查询所有员工信息
query_all_employees()
# 添加员工信息
add_employee("张三", 25, "男", "13888888888", "zhangsan@example.com", "北京市海淀区", 1, 1)
# 修改员工信息
update_employee(1, "李四", 30, "男", "13999999999", "lisi@example.com", "北京市朝阳区", 2, 2)
# 删除员工信息
delete_employee(1)
# 查询所有专业信息
query_all_professions()
# 添加专业信息
add_profession("Java开发")
# 修改专业信息
update_profession(1, "Python开发")
# 删除专业信息
delete_profession(1)
# 查询所有部门信息
query_all_depts()
# 添加部门信息
add_dept("研发部")
# 修改部门信息
update_dept(1, "技术部")
# 删除部门信息
delete_dept(1)
```
这样,我们就完成了一个简单的员工信息管理系统。
阅读全文