赞
踩
利用Python完成一个学生信息的增删改查程序,主要涉及知识点:程序控制结构(实现可进可退的多级菜单)、文件读写操作(学生信息要保存到磁盘上的文件里)、利用函数实现功能模块化。
2021001 李晓红 女 19 2021级软件4班 软件技术 人工智能与大数据学院 15945456780
2021002 王晓刚 男 18 2021级软件4班 软件技术 人工智能与大数据学院 13890904567
2021003 唐雨涵 女 19 2021级软件4班 软件技术 人工智能与大数据学院 18878789023
2021101 张三丰 男 18 2021级大数据1班 大数据技术 人工智能与大数据学院 15945456780
2021102 肖雨林 男 18 2021级大数据1班 大数据技术 人工智能与大数据学院 18890904560
2021103 郑小翠 女 19 2021级大数据1班 大数据技术 人工智能与大数据学院 15890904567
# 主程序
while True:
print('用户登录')
print('==========')
print('1.登录')
print('2.退出')
print('==========')
mc1 = int(input('输入菜单号: '))
if mc1 == 1:
print('成功登入!')
elif mc1 == 2:
print('\n谢谢使用本程序\n')
break
运用def定义函数创建学生管理系统二级菜单并调用。
运行程序,查看结果
定义add_student()函数,并调用。
运行查看结果,并录入新的信息。
为查询全部学生定义一条函数dis_all_stu()
运行程序查看结果
成功显示所有学生信息。
因为为了书写代码简洁明了,于是把这这条链接到数据库的代码放在了最开头,这样就不用在每次定义的时候再去书写一遍。
为进行按学号查询学生记录,定义一条id_dis_stu()函数
运行程序,查看结果
成功按学号显示学生信息
为进行按姓名查询学生记录,定义一条name_dis_stu()函数
运行程序,查看结果
成功按学号显示学生信息.
为进行姓名信息修改,定义一条modify_stu_name()函数
运行程序,查看结果
成功修改姓名
利用查询功能查看是否修改成功
操作成功!
# -*- coding: utf-8 -*- """ 功能:学生管理系统 作者:zwh 日期:2021年12月7日 """ import pymysql # 定义数据链接参数 host = '127.0.0.1' port = 3306 db = 'student' user = 'root' password = 'zl202111' # 获取数据链接 conn = pymysql.connect(host=host, port=port, db=db, user=user, password=password) cursor = conn.cursor(pymysql.cursors.SSCursor) def add_student(): # 添加学生信息 sql = 'insert into students values(%s, %s, %s, %s, %s, %s, %s, %s )' print('录入信息:') id = int(input('id:')) name = input('name:') gender = input('gender:') age = int(input('age:')) class_grade = input('class_grade:') major = input(('major:')) college = input('college:') telephone = input('telephone:') row = (id, name, gender, age, class_grade, major, college, telephone) print(row) count = cursor.execute(sql, row) if count > 0: # 提交数据修改 conn.commit() # 提示用户操作成功 print('记录插入成功!') else: # 提示操作失败 print('记录插入失败!') def dis_all_stu(): # 查询所有学生信息 cursor.execute('select * from students ') # 获取全部学生数据 students = cursor.fetchall() # print(students) for stu in students: # print(students[i]) print('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(stu[0], stu[1], stu[2], stu[3], stu[4],stu[5], stu[6], stu[7])) def id_dis_stu(): # 以学号查询学生信息 # 执行SQL查询 id = input('输入待查id:') cursor.execute('select * from students where id = %s', (id)) # 获取全部学生数据 student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: for i in student: print('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7])) def name_dis_stu(): # 以姓名查询学生信息 # 执行SQL查询 name = input('输入待查name:') cursor.execute('select * from students where name = %s', (name)) # 获取全部学生数据 student = cursor.fetchall() if student == []: print(f'未查到姓名为{name}的学生!') for i in student: print('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7])) # 修改学生姓名 def modify_stu_name(): while True: id = input('输入待修改信息的id:') cursor.execute('select * from students where id = %s', (id)) student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: new_name = input('输入修改后的姓名:') count = cursor.execute('update students set name = %s where id = %s', (new_name, id)) if count > 0: conn.commit() print('数据修改成功!') break else: print('数据修改失败!') def modify_stu_gender(): while True: id = input('输入待修改信息的id:') cursor.execute('select * from students where id = %s', (id)) student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: new_gender = input('输入修改后的性别:') count = cursor.execute('update students set gender = %s where id = %s', (new_gender, id)) if count > 0: conn.commit() print('数据修改成功!') break else: print('数据修改失败!') def modify_stu_age(): while True: id = input('输入待修改信息的id:') cursor.execute('select * from students where id = %s', (id)) student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: new_age = input('输入修改后的年龄:') count = cursor.execute('update students set age = %s where id = %s', (new_age, id)) if count > 0: conn.commit() print('数据修改成功!') break else: print('数据修改失败!') def modify_stu_cg(): while True: id = input('输入待修改信息的id:') cursor.execute('select * from students where id = %s', (id)) student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: new_class_grade = input('输入修改后的班级:') count = cursor.execute('update students set class_grade = %s where id = %s', (new_class_grade, id)) if count > 0: conn.commit() print('数据修改成功!') break else: print('数据修改失败!') def modify_stu_major(): while True: id = input('输入待修改信息的id:') cursor.execute('select * from students where id = %s', (id)) student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: new_major = input('输入修改后的专业:') count = cursor.execute('update students set major = %s where id = %s', (new_major, id)) if count > 0: conn.commit() print('数据修改成功!') break else: print('数据修改失败!') def modify_stu_college(): while True: id = input('输入待修改信息的id:') cursor.execute('select * from students where id = %s', (id)) student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: new_college = input('输入修改后的学院:') count = cursor.execute('update students set college = %s where id = %s', (new_college, id)) if count > 0: conn.commit() print('数据修改成功!') break else: print('数据修改失败!') def modify_stu_phone(): while True: id = input('输入待修改信息的id:') cursor.execute('select * from students where id = %s', (id)) student = cursor.fetchall() if student == []: print(f'未查到学号为{id}的学生!') else: new_phone = input('输入修改后的电话:') count = cursor.execute('update students set telephone = %s where id = %s', (new_phone, id)) if count > 0: conn.commit() print('数据修改成功!') break else: print('数据修改失败!') def modify_student(): # 修改学生信息 while True: print('\n查询学生记录\n') print('=================') print('1.修改姓名') print('2.修改性别') print('3.修改年龄') print('4.修改班级') print('5.修改专业') print('6.修改学院') print('7.修改电话') print('8.返回上级菜单') print('=================') mc4 = int(input('输入菜单号:')) if mc4 == 1: modify_stu_name() elif mc4 == 2: modify_stu_gender() elif mc4 == 3: modify_stu_age() elif mc4 == 4: modify_stu_cg() elif mc4 == 5: modify_stu_major() elif mc4 == 6: modify_stu_college() elif mc4 == 7: modify_stu_phone() else: break def query_student(): # 查询学生记录 while True: print('\n查询学生记录\n') print('=================') print('1.按学号查询学生记录') print('2.按姓名查询学生记录') print('3.查询全部学生记录') print('4.返回上级菜单') print('=================') mc3 = int(input('输入菜单号:')) if mc3 == 1: id_dis_stu() elif mc3 == 2: name_dis_stu() elif mc3 == 3: dis_all_stu() else: break # 删除信息 def del_stu(): # 执行SQL查询 id = input('输入待查学号:') count = cursor.execute('delete from students where id = %s', (id)) if count > 0: # 提交数据修改 conn.commit() print('记录删除成功!') else: print('记录删除失败!') def login(): username = input('输入用户名: ') password = input('输入密码: ') if username == 'zl' and password == '0': while True: print('\n学生信息管理\n') print('===========') print('1. 增加学生记录') print('2. 查询学生记录') print('3. 修改学生记录') print('4. 删除学生记录') print('5. 返回上级菜单') print('===========') mc2 = int(input('输入菜单号: ')) if mc2 == 1: add_student() elif mc2 == 2: query_student() elif mc2 == 3: modify_student() elif mc2 == 4: del_stu() else: break else: print('\n用户名或密码错误,请重新登录\n') # 主程序 while True: print('用户登录') print('==========') print('1.登录') print('2.退出') print('==========') mc1 = int(input('输入菜单号: ')) if mc1 == 1: login() elif mc1 == 2: print('\n谢谢使用本程序\n') break
while True:
print('用户登录')
print('==========')
print('1.登录')
print('2.退出')
print('==========')
mc1 = input('输入菜单号: ')
if mc1 == str(1):
login()
elif mc1 == str(2):
print('\n谢谢使用本程序\n')
break
elif mc1 != 1 or mc1 != 2:
print('请输入正确的菜单号!')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。