学员健康管理系统 大作业
欢迎阅读本篇博客,我们将介绍如何优化一个基于 Python 和 MySQL 的用户管理系统。该系统旨在提供管理员和普通用户角色的功能,并包括用户管理、体检报告记录、医学知识库等功能。通过本指南,您将了解系统的重点功能以及如何结合代码进行解释。
1. 数据库连接
首先,我们使用 pymysql 模块来连接 MySQL 数据库。连接数据库是系统运行的基础,因此这一步尤为重要。
import pymysql
# 连接数据库
conn = pymysql.connect(
host='localhost',
user='root',
port=3306,
password='12345678',
database='users'
)
2. 登录功能
用户可以通过输入学号和密码进行登录验证。系统将查询数据库以验证用户凭据的有效性,并根据结果跳转到相应的菜单页面。
def login():
student_number = input("请输入学号: ")
password = input("请输入密码: ")
# 查询数据库中是否存在该用户
sql = "SELECT * FROM users WHERE student_number = %s AND password = %s"
cursor.execute(sql, (student_number, password))
result = cursor.fetchone()
if result:
print("登录成功!")
# 根据用户信息跳转到不同的方法
if result[0] == 0 or result[0] == '0':
admin_menu(result[0])
else:
user_menu(result[0])
else:
print("用户名或密码错误!")
login()
3. 管理员菜单
管理员菜单提供了用户管理功能,包括修改用户密码、删除用户、添加用户等。管理员可以根据需要执行相应的操作。
def admin_menu(user):
print(f"欢迎{user}管理员!")
while True:
print("请选择功能:")
print("1. 用户管理")
print("0. 退出")
choice = input("请输入选项:")
if choice == '1':
student_manager(user)
elif choice == '0':
break
else:
print("无效选项,请重新输入!")
4. 普通用户菜单
普通用户菜单提供了学员区队、学员信息、体检项目、体检报告、医学知识库等功能选项。用户可以根据需要选择相应的功能进行操作。
def user_menu(user):
print(f"欢迎{user}用户!")
while True:
print("请选择功能:")
print("1. 学员区队")
print("2. 学员信息")
print("3. 体检项目")
print("4. 体检报告")
print("5. 医学知识库")
print("6. 密码修改")
print("0. 退出")
choice = input("请输入选项:")
if choice == '1':
student_team(user)
elif choice == '2':
student_info(user)
elif choice == '3':
health_exam(user)
elif choice == '4':
health_report(user)
elif choice == '5':
medical_knowledge()
elif choice == '6':
password_change(user)
elif choice == '0':
break
else:
print("无效选项,请重新输入!")
下面是学院健康管理系统的完整代码:
import pymysql
# 连接数据库
conn = pymysql.connect(
host='localhost',
user='root',
port=3306,
password='12345678',
database='users'
)
# 创建游标对象
cursor = conn.cursor()
def login():
student_number = input("请输入学号: ")
password = input("请输入密码: ")
# 查询数据库中是否存在该用户
sql = "SELECT * FROM users WHERE student_number = %s AND password = %s"
cursor.execute(sql, (student_number, password))
result = cursor.fetchone()
if result:
print("登录成功!")
# 根据用户信息跳转到不同的方法
if result[0] == 0 or result[0] == '0':
admin_menu(result[0])
else:
user_menu(result[0])
else:
print("用户名或密码错误!")
login()
def admin_menu(user):
print(f"欢迎{user}管理员!")
# 在这里实现管理员菜单的功能
while True:
print("请选择功能:")
print("1. 用户管理")
print("0. 退出")
choice = input("请输入选项:")
if choice == '1':
student_manger(user)
elif choice == '0':
break
else:
print("无效选项,请重新输入!")
def student_manger(user):
sql = "SELECT * FROM users "
cursor.execute(sql,)
results = cursor.fetchall()
if results:
print("用户信息如下:")
for row in results:
print(f"账号: {row[0]} 密码: {row[1]}\n")
while True:
print("请选择功能:")
print("1. 修改用户")
print("2. 删除用户")
print("3. 增加用户")
print("0. 退出")
choice = input("请输入选项:")
if choice == '1':
student_change()
elif choice == '2':
student_delete()
elif choice == '3':
student_add()
elif choice == '0':
break
else:
print("无该功能!")
else:
print("无用户!")
def student_add():
print("增加用户")
# 在这里实现体检报告功能的代码
exam_name = input("请输入用户名:")
exam_password = input("请输入密码:")
sql = "SELECT * FROM users where student_number = %s "
cursor.execute(sql, (exam_name, ))
results = cursor.fetchall()
if results:
print("用户已存在")
else:
sql = "INSERT INTO users (student_number, password) VALUES (%s, %s)"
cursor.execute(sql, (exam_name, exam_password))
conn.commit()
print("添加完毕")
def student_change():
print("用户修改")
# 在这里实现体检报告功能的代码
exam_result = input("请输入修改用户名:")
sql = "SELECT * FROM users where student_number = %s "
cursor.execute(sql, (exam_result,))
results = cursor.fetchall()
if results:
password_result = input("请输入修改密码:")
sql = "UPDATE users SET password = %s WHERE student_number = %s"
cursor.execute(sql, (password_result,exam_result,))
conn.commit()
print("修改成功")
else:
print("该用户不存在")
def student_delete():
print("用户删除")
# 在这里实现体检报告功能的代码
exam_result = input("请输入删除用户名:")
sql = "SELECT * FROM users where student_number = %s "
cursor.execute(sql, (exam_result,))
results = cursor.fetchall()
if results:
sql = "DELETE FROM users where student_number = %s "
cursor.execute(sql, (exam_result,))
conn.commit()
print("删除成功")
else:
print("该用户不存在")
def user_menu(user):
print(f"欢迎{user}用户!")
while True:
print("请选择功能:")
print("1. 学员区队")
print("2. 学员信息")
print("3. 体检项目")
print("4. 体检报告")
print("5. 医学知识库")
print("6. 密码修改")
print("0. 退出")
choice = input("请输入选项:")
if choice == '1':
student_team(user)
elif choice == '2':
student_info(user)
elif choice == '3':
health_exam(user)
elif choice == '4':
health_report(user)
elif choice == '5':
medical_knowledge()
elif choice == '6':
password_change(user)
elif choice == '0':
break
else:
print("无效选项,请重新输入!")
def student_team(user):
print("学员区队功能")
# 在这里实现学员区队功能的代码
sql = "SELECT * FROM students where student_number = %s"
cursor.execute(sql,(user,))
results = cursor.fetchall()
if results:
print("区队信息如下:")
for row in results:
print(f"学号: {row[0]}\n姓名: {row[4]}\n区队名: {row[1]}\n负责人: {row[2]}\n负责人联系方式: {row[3]}\n")
else:
print("学员信息为空")
def student_info(user):
print("学员信息功能")
# 在这里实现学员信息功能的代码
sql = "SELECT * FROM students where student_number = %s"
cursor.execute(sql,(user,))
results = cursor.fetchall()
if results:
print("区队信息如下:")
for row in results:
print(f"学号: {row[0]}\n姓名: {row[4]}\n入学时间: {row[5]}\n年龄: {row[6]}\n")
else:
print("学员信息为空")
def health_exam(user):
print("体检项目功能")
# 在这里实现体检项目功能的代码
while True:
print("请选择功能:")
print("1. 外科")
print("2. 心电图")
print("3. 血常规")
print("4. 尿常规")
print("5. b超")
print("0. 退出")
choice = input("请输入选项:")
if choice == '1':
student_A(user)
elif choice == '2':
student_B(user)
elif choice == '3':
student_C(user)
elif choice == '4':
student_D(user)
elif choice == '5':
student_E(user)
elif choice == '0':
break
else:
print("无效选项,请重新输入!")
def password_change(user):
print("密码修改")
# 在这里实现体检报告功能的代码
exam_result = input("请输入修改密码:")
sql = "SELECT * FROM users where student_number = %s "
cursor.execute(sql, (user,))
results = cursor.fetchall()
if results:
sql = "UPDATE users SET password = %s WHERE student_number = %s"
cursor.execute(sql, (exam_result, user,))
conn.commit()
print("修改成功")
else:
print("修改失败")
def student_A(user):
print("外科检查")
# 在这里实现体检报告功能的代码
exam_name = '外科'
exam_method = '体检'
normal_range = '90'
exam_result = input("请输入检查结果:")
sql = "SELECT * FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name, ))
results = cursor.fetchall()
if results:
sql = "UPDATE health_exams SET exam_result = %s WHERE student_number = %s AND exam_name = %s"
cursor.execute(sql, (exam_result,user, exam_name,))
conn.commit()
else:
sql = "INSERT INTO health_exams (student_number, exam_name, exam_method,normal_range, exam_result) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (user, exam_name, exam_method, normal_range, exam_result))
conn.commit()
print("外科体检成功")
def student_B(user):
print("心电图检查")
# 在这里实现体检报告功能的代码
exam_name = '心电图'
exam_method = '体检'
normal_range = '90'
exam_result = input("请输入检查结果:")
sql = "SELECT * FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name, ))
results = cursor.fetchall()
if results:
sql = "UPDATE health_exams SET exam_result = %s WHERE student_number = %s AND exam_name = %s"
cursor.execute(sql, (exam_result,user, exam_name,))
conn.commit()
else:
sql = "INSERT INTO health_exams (student_number, exam_name, exam_method,normal_range, exam_result) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (user, exam_name, exam_method, normal_range, exam_result))
conn.commit()
print("心电图体检成功")
def student_C(user):
print("血常规检查")
# 在这里实现体检报告功能的代码
exam_name = '血常规'
exam_method = '体检'
normal_range = '90'
exam_result = input("请输入检查结果:")
sql = "SELECT * FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name, ))
results = cursor.fetchall()
if results:
sql = "UPDATE health_exams SET exam_result = %s WHERE student_number = %s AND exam_name = %s"
cursor.execute(sql, (exam_result,user, exam_name,))
conn.commit()
else:
sql = "INSERT INTO health_exams (student_number, exam_name, exam_method,normal_range, exam_result) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (user, exam_name, exam_method, normal_range, exam_result))
conn.commit()
print("血常规体检成功")
def student_D(user):
print("尿常规检查")
# 在这里实现体检报告功能的代码
exam_name = '尿常规'
exam_method = '体检'
normal_range = '90'
exam_result = input("请输入检查结果:")
sql = "SELECT * FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name, ))
results = cursor.fetchall()
if results:
sql = "UPDATE health_exams SET exam_result = %s WHERE student_number = %s AND exam_name = %s"
cursor.execute(sql, (exam_result,user, exam_name,))
conn.commit()
else:
sql = "INSERT INTO health_exams (student_number, exam_name, exam_method,normal_range, exam_result) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (user, exam_name, exam_method, normal_range, exam_result))
conn.commit()
print("尿常规体检成功")
def student_E(user):
print("b超检查")
# 在这里实现体检报告功能的代码
exam_name = 'b超'
exam_method = '体检'
normal_range = '90'
exam_result = input("请输入检查结果:")
sql = "SELECT * FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name, ))
results = cursor.fetchall()
if results:
sql = "UPDATE health_exams SET exam_result = %s WHERE student_number = %s AND exam_name = %s"
cursor.execute(sql, (exam_result,user, exam_name,))
conn.commit()
else:
sql = "INSERT INTO health_exams (student_number, exam_name, exam_method,normal_range, exam_result) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (user, exam_name, exam_method, normal_range, exam_result))
conn.commit()
print("b超体检成功")
def health_report(user):
print("体检报告功能")
# 在这里实现体检报告功能的代码
sql = "SELECT * FROM health_exams where student_number = %s"
cursor.execute(sql,(user,))
results = cursor.fetchall()
if results:
print("体检信息如下:")
print(f"学号:{user}")
for row in results:
print(f"项目名称: {row[1]}\n检查方式: {row[2]}\n正常值范围: {row[3]}\n检查结果:{row[4]}")
while True:
print("请选择功能:")
print("1. 修改体检报告")
print("2. 删除体检报告")
print("0. 退出")
choice = input("请输入选项:")
if choice == '1':
student_F(user)
elif choice == '2':
student_G(user)
elif choice == '0':
break
else:
print("无效选项,请重新输入!")
else:
print(f"{user}学员未体检")
def student_F(user):
print("体检报告修改功能")
# 在这里实现体检报告功能的代码
exam_name = input("请输入项目名称:")
sql = "SELECT * FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name,))
results = cursor.fetchall()
if results:
exam_result = input("请输入检查结果:")
sql = "UPDATE health_exams SET exam_result = %s WHERE student_number = %s AND exam_name = %s"
cursor.execute(sql, (exam_result, user, exam_name,))
conn.commit()
print("修改成功")
else:
print(f"{exam_name}项目还未体检")
def student_G(user):
print("体检报告删除功能")
# 在这里实现体检报告功能的代码
exam_name = input("请输入项目名称:")
sql = "SELECT * FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name,))
results = cursor.fetchall()
if results:
sql = "DELETE FROM health_exams where student_number = %s AND exam_name = %s"
cursor.execute(sql, (user, exam_name,))
conn.commit()
print("删除成功")
else:
print(f"{exam_name}项目还未体检")
def medical_knowledge():
print("医学知识库功能")
# 在这里实现体检报告功能的代码
# 执行登录函数
login()
# 关闭游标和连接
cursor.close()
conn.close()
如有遇到问题可以找小编沟通交流哦。另外小编帮忙辅导大课作业,学生毕设等。不限于python,java,大数据,模型训练等。