Python职工管理系统代码
时间: 2024-06-15 12:01:30 浏览: 207
创建一个Python员工管理系统代码通常会涉及到多个模块,包括数据库操作、用户管理、员工信息管理等功能。这里我会提供一个简化的概念和基本结构,但请注意在实际项目中,你可能需要使用更复杂的库(如Django、Flask或SQLAlchemy等)以及数据持久化技术。
```python
# 引入所需库
from flask import Flask, request, jsonify
import sqlite3
# 初始化Flask应用
app = Flask(__name__)
# 连接到SQLite数据库(这里简化为内存数据库)
db_connection = sqlite3.connect('employees.db')
cursor = db_connection.cursor()
# 员工模型
class Employee:
def __init__(self, id, name, position):
self.id = id
self.name = name
self.position = position
# 员工管理函数
def add_employee(name, position):
cursor.execute("INSERT INTO employees (name, position) VALUES (?, ?)", (name, position))
db_connection.commit()
return "Employee added"
def get_employees():
cursor.execute("SELECT * FROM employees")
return [dict(row) for row in cursor.fetchall()]
def update_employee(employee_id, new_name, new_position):
cursor.execute("UPDATE employees SET name=?, position=? WHERE id=?", (new_name, new_position, employee_id))
db_connection.commit()
return "Employee updated"
# API路由
@app.route('/add_employee', methods=['POST'])
def add_employee_api():
data = request.get_json()
return add_employee(data['name'], data['position'])
@app.route('/employees', methods=['GET'])
def get_employees_api():
return jsonify(get_employees())
@app.route('/update_employee/<int:employee_id>', methods=['PUT'])
def update_employee_api(employee_id):
data = request.get_json()
return update_employee(employee_id, data['name'], data['position'])
# 启动服务器
if __name__ == '__main__':
app.run(debug=True)
阅读全文