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 相关文章推荐
Python去除列表中重复元素的方法
Mar 20 Python
在Python程序中操作MySQL的基本方法
Jul 29 Python
python简单图片操作:打开\显示\保存图像方法介绍
Nov 23 Python
对python字典元素的添加与修改方法详解
Jul 06 Python
python3结合openpyxl库实现excel操作的实例代码
Sep 11 Python
解决PyCharm的Python.exe已经停止工作的问题
Nov 29 Python
python 实现返回一个列表中出现次数最多的元素方法
Jun 11 Python
如何运行.ipynb文件的图文讲解
Jun 27 Python
原来我一直安装 Python 库的姿势都不对呀
Nov 11 Python
Python @property原理解析和用法实例
Feb 11 Python
python map比for循环快在哪
Sep 21 Python
python接口自动化框架实战
Dec 23 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
基于文本的留言簿
2006/10/09 PHP
phpmyadmin 3.4 空密码登录的实现方法
2010/05/29 PHP
PHP flock 文件锁详细介绍
2012/12/29 PHP
PHP 使用header函数设置HTTP头的示例解析 表头
2013/06/17 PHP
ThinkPHP3.2.2的插件控制器功能简述
2014/07/09 PHP
JavaScript 基于原型的对象(创建、调用)
2009/10/16 Javascript
中文路径导致unitpngfix.js不正常的解决方法
2013/06/26 Javascript
使用Js让Html中特殊字符不被转义
2013/11/05 Javascript
Jquery图片延迟加载插件jquery.lazyload.js的使用方法
2014/05/21 Javascript
javascript框架设计读书笔记之种子模块
2014/12/02 Javascript
jQuery中的一些常见方法小结(推荐)
2016/06/13 Javascript
jQuery Mobile漏洞会有跨站脚本攻击风险
2017/02/12 Javascript
javascript实现获取一个日期段内每天不同的价格(计算入住总价格)
2018/02/05 Javascript
jQuery实现鼠标响应式淘宝动画效果示例
2018/02/13 jQuery
JS高阶函数原理与用法实例分析
2019/01/15 Javascript
Vue组件之高德地图地址选择功能的实例代码
2019/06/21 Javascript
jQuery实现可以计算进制转换的计算器
2020/10/19 jQuery
[06:53]2018DOTA2国际邀请赛寻真——为复仇而来的Newbee
2018/08/15 DOTA
python计算方程式根的方法
2015/05/07 Python
在Python中使用next()方法操作文件的教程
2015/05/24 Python
CentOS下Python3的安装及创建虚拟环境的方法
2018/11/28 Python
python将txt文档每行内容循环插入数据库的方法
2018/12/28 Python
解决Python对齐文本字符串问题
2019/08/28 Python
python实现淘宝购物系统
2019/10/25 Python
通过实例了解Python str()和repr()的区别
2020/01/17 Python
Python tcp传输代码实例解析
2020/03/18 Python
在python中实现求输出1-3+5-7+9-......101的和
2020/04/02 Python
Bally澳大利亚官网:瑞士奢侈品牌
2018/11/01 全球购物
优秀教师获奖感言
2014/01/31 职场文书
教师校本培训方案
2014/02/26 职场文书
2015年女生节活动总结
2015/02/27 职场文书
2015年党总支工作总结
2015/05/25 职场文书
go设置多个GOPATH的方式
2021/05/05 Golang
CSS 实现多彩、智能的阴影效果
2021/05/12 HTML / CSS
sass 常用备忘案例详解
2021/09/15 HTML / CSS
js中Object.create实例用法详解
2021/10/05 Javascript