Python基于mysql实现学生管理系统


Posted in Python onFebruary 21, 2019

本篇文章主要介绍了Python基于mysql实现学生管理系统,分享给大家,具体如下:

import pymysql
import re
 
def idinput(string):
 ID = input(string)
 pattern = re.compile("^\d{1,3}$")
 while not re.match(pattern, ID):
  ID = input("请输入1-3位整数:")
 return ID
 
def appendStudentInfo():
 ID =idinput("请输入学生学号:")
 db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
 cursor=db.cursor()
 sql = "select * from StuSys where ID = '%s'" % ID
 cursor.execute(sql)
 while cursor.rowcount > 0 :
  ID = idinput("该学号已存在,请重新输入:")
  sql = "select * from StuSys where ID = '%d'" % int(ID)
  cursor.execute(sql)
 
 name=input("请输入学生姓名:")
 
 chinese=input("请输入语文成绩:")
 while not chinese.isdigit() or int(chinese)>100 or int(chinese)<0:
  chinese = input("输入错误,请重新输入:")
 
 math =input("请输入数学成绩:")
 while not math.isdigit() or int(math) > 100 or int(math) < 0:
  math = input("输入错误,请重新输入:")
 
 english=input("请输入英语成绩:")
 while not english.isdigit() or int(english) > 100 or int(english) < 0:
  english = input("输入错误,请重新输入:")
 
 total=int(chinese)+int(math)+int(english)
 
 sql="""INSERT INTO StuSys(ID,
   NAME,CHINESE,ENGLISH,MATH,TOTAL)
   VALUES (%s,%s,%s,%s,%s,%s)"""
 cursor.execute(sql,(ID,name,chinese,english,math,total))
 db.commit()
 db.close()
 
def delstudent():
 delstudentid = idinput("请输入要删除的学生学号:")
 if querystudent(delstudentid):
  select = input("是否删除:是(Y)/否(N)")
  if select == "Y" or select == "y":
   db = pymysql.connect(host="127.0.0.1", user="root", passwd="hisense", db="test", port=3306, charset="utf8")
   cursor = db.cursor()
   sql = "delete from stusys where ID =%s" %delstudentid
   cursor.execute(sql)
   db.commit()
   db.close()
   print("删除成功")
  elif select == "N" or select == "n":
   print("取消删除")
  else:
   print("输入错误")
 
 
def querystudent(querystudentid):
 db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
 cursor=db.cursor()
 sql="select * from stusys where ID=%s"%querystudentid
 cursor.execute(sql)
 if cursor.rowcount ==0 :
  print("不存在该学生信息")
  return False
 else:
  print("该学生信息如下:")
  results =cursor.fetchall()
  print("ID=%d,NAME=%s,CHINESE=%d,ENGLISH=%d,MATH=%d,TOTAL=%d" % \
   (results[0][0], results[0][1], results[0][2], results[0][3], results[0][4],results[0][5]))
  return True
 
def modifystudentifo():
 modifyid = idinput("请输入要的学生学号:")
 if querystudent(modifyid):
  name = input("请重新输入学生姓名:")
 
  chinese = input("请重新输入语文成绩:")
  while not chinese.isdigit() or int(chinese) > 100 or int(chinese) < 0:
   chinese = input("输入错误,请重新输入:")
 
  math = input("请重新输入数学成绩:")
  while not math.isdigit() or int(math) > 100 or int(math) < 0:
   math = input("输入错误,请重新输入:")
 
  english = input("请重新输入英语成绩:")
  while not english.isdigit() or int(english) > 100 or int(english) < 0:
   english = input("输入错误,请重新输入:")
 
  total = int(chinese) + int(math) + int(english)
  db = pymysql.connect(host="127.0.0.1", user="root", passwd="hisense", db="test", port=3306, charset="utf8")
  cursor = db.cursor()
  sql1="update stusys set name ='%s' where id = %s"%(name,modifyid)
  cursor.execute(sql1)
  sql2="update stusys set math = %s where id = %s"%(math,modifyid)
  cursor.execute(sql2)
  sql3 = "update stusys set english = %s where id =%s"%(english,modifyid)
  cursor.execute(sql3)
  sql4 = "update stusys set total = %s where id = %s"%(total,modifyid)
  cursor.execute(sql4)
  sql5 = "update stusys set chinese = %s where id = %s"%(chinese,modifyid)
  cursor.execute(sql5)
  db.commit()
  db.close()
 
def allinfo():
 db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
 cursor=db.cursor()
 sql="select * from stusys"
 cursor.execute(sql)
 results= cursor.fetchall()
 for row in results:
  ID = row[0]
  NAME = row[1]
  CHINESE = row[2]
  ENGLISH = row[3]
  MATH = row[4]
  TOTAL = row[5]
  # 打印结果
  print("ID=%d,NAME=%s,CHINESE=%d,ENGLISH=%d,MATH=%d,TOTAL=%d" % \
    (ID, NAME, CHINESE, ENGLISH, MATH,TOTAL))
 
def studentMenu():
 print("="*30)
 print("学生管理系统")
 print("1、添加学生信息")
 print("2、删除学生信息")
 print("3、查询学生信息")
 print("4、修改学生信息")
 print("5、全部学生信息")
 print("6、退出")
 print("="*30)
 
 
 
if __name__ == '__main__':
 
 while True:
  studentMenu()
  menuindex = input("请输入选项序号:")
  while not menuindex.isdigit():
   menuindex = input("输入错误,请重新输入:")
  if int(menuindex) ==1:
   appendStudentInfo()
  elif int(menuindex) ==2:
   delstudent()
  elif int(menuindex) ==3:
   querystudentid = idinput("请输入要查询的学生学号:")
   querystudent(querystudentid)
  elif int(menuindex) ==4:
    modifystudentifo()
  elif int(menuindex) == 5:
    allinfo()
  elif int(menuindex) == 6:
   break
  else:
   print("输入序号无效")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python3访问并下载网页内容的方法
Jul 28 Python
ubuntu安装sublime3并配置python3环境的方法
Mar 15 Python
Anaconda 离线安装 python 包的操作方法
Jun 11 Python
Python走楼梯问题解决方法示例
Jul 25 Python
让代码变得更易维护的7个Python库
Oct 09 Python
基于Python实现迪杰斯特拉和弗洛伊德算法
May 27 Python
python找出一个列表中相同元素的多个索引实例
Jun 11 Python
Python3安装psycopy2以及遇到问题解决方法
Jul 03 Python
利用Python进行图像的加法,图像混合(附代码)
Jul 14 Python
python使用 cx_Oracle 模块进行查询操作示例
Nov 28 Python
使用Python paramiko模块利用多线程实现ssh并发执行操作
Dec 05 Python
浅谈python print(xx, flush = True) 全网最清晰的解释
Feb 21 Python
python+mysql实现教务管理系统
Feb 20 #Python
python Tkinter版学生管理系统
Feb 20 #Python
啥是佩奇?使用Python自动绘画小猪佩奇的代码实例
Feb 20 #Python
Python实战购物车项目的实现参考
Feb 20 #Python
利用django+wechat-python-sdk 创建微信服务器接入的方法
Feb 20 #Python
python3+pyqt5+itchat微信定时发送消息的方法
Feb 20 #Python
钉钉群自定义机器人消息Python封装的实例
Feb 20 #Python
You might like
教你在PHPStorm中配置Xdebug
2015/07/27 PHP
微信支付开发订单查询实例
2016/07/12 PHP
Yii核心验证器api详解
2016/11/23 PHP
简单实用的PHP文本缓存类实例
2019/03/22 PHP
javascript的回调函数应用示例
2014/02/20 Javascript
jQuery中:file选择器用法实例
2015/01/04 Javascript
JavaScript实现网页对象拖放功能的方法
2015/04/15 Javascript
js贪吃蛇网页版游戏特效代码分享(挑战十关)
2015/08/24 Javascript
JavaScript通过使用onerror设置默认图像显示代替alt
2016/03/01 Javascript
基于jquery实现智能提示控件intellSeach.js
2016/03/17 Javascript
js 能实现监听F5页面刷新子iframe 而父页面不刷新的方法
2016/11/09 Javascript
Bootstrap popover用法详解
2016/12/22 Javascript
深入探究angular2 UI组件之primeNG用法
2017/07/26 Javascript
vue项目中使用多选框的实例代码
2020/07/22 Javascript
Python实现简单的获取图片爬虫功能示例
2017/07/12 Python
python实现mysql的读写分离及负载均衡
2018/02/04 Python
对python 操作solr索引数据的实例详解
2018/12/07 Python
pycharm 实现显示project 选项卡的方法
2019/01/17 Python
Python中的异常处理try/except/finally/raise用法分析
2019/02/28 Python
python中栈的原理及实现方法示例
2019/11/27 Python
Python udp网络程序实现发送、接收数据功能示例
2019/12/09 Python
Django框架配置mysql数据库实现过程
2020/04/22 Python
python文件排序的方法总结
2020/09/13 Python
css3的focus-within选择器的使用
2020/05/11 HTML / CSS
加拿大便宜的隐形眼镜商店:Clearly
2016/09/15 全球购物
时尚设计师手表:The Watch Cabin
2018/10/06 全球购物
英国排名第一的宠物店:PetPlanet
2020/02/02 全球购物
共产党员岗位承诺书
2014/05/29 职场文书
八项规定自查自纠报告及整改措施
2014/10/26 职场文书
2014年教育培训工作总结
2014/12/08 职场文书
转正申请报告格式
2015/05/15 职场文书
贷款收入证明格式
2015/06/24 职场文书
领导干部学习三严三实心得体会
2016/01/05 职场文书
新西兰:最新留学学习计划书写作指南
2019/07/15 职场文书
python中redis包操作数据库的教程
2022/04/19 Python
windows系统搭建WEB服务器详细教程
2022/08/05 Servers