当前位置: 首页 > 编程笔记 >

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

闾丘朗
2023-03-14
本文向大家介绍Python基于mysql实现学生管理系统,包括了Python基于mysql实现学生管理系统的使用技巧和注意事项,需要的朋友参考一下

本篇文章主要介绍了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实现学生信息管理系统的使用技巧和注意事项,需要的朋友参考一下 学生信息管理系统负责编辑学生信息,适时地更新学生的资料。下面通过python实现一个简单的学生信息管理系统 调用main()函数即可: 虽然以上代码可以完成上述功能,但是每次调用程序里面的数据全部清除掉了,所以为了更好的模拟现实的学生信息管理系统,我们还需要将上次

  • 本文向大家介绍基于Python实现简单学生管理系统,包括了基于Python实现简单学生管理系统的使用技巧和注意事项,需要的朋友参考一下 学生信息管理系统负责编辑学生信息,供大家参考,具体内容如下 第一次发帖,下面通过python实现一个简单的学生信息管理系统 要求如下: 1.添加学生的信息 2.删除学生的信息 3.修改的信息 4.查询学生的信息 5.遍历学生的信息 6.退出系统 写法: 1.先考虑

  • 本文向大家介绍用python实现学生管理系统,包括了用python实现学生管理系统的使用技巧和注意事项,需要的朋友参考一下 学生管理系统 相信大家学各种语言的时候,练习总是会写各种管理系统吧,管理系统主要有对数据的增删查改操作,原理不难,适合作为练手的小程序 数据的结构 要保存数据就需要数据结构,比如c里面的结构体啊,python里面的列表,字典,还有类都是常用的数据类型 在这里,我使用了链表来作

  • 本文向大家介绍python实现学生管理系统开发,包括了python实现学生管理系统开发的使用技巧和注意事项,需要的朋友参考一下 使用python完成超级基础的学生管理系统,供大家参考,具体内容如下 说明: 1、本学生管理系统非常非常简易,只有增,显,查,删,改功能,对于Python新手容易看懂上手。 2、信息的存储只使用了字典和列表。 3、不喜勿喷。 代码: 1、主循环框架 2、源代码 总结 1、

  • 本文向大家介绍基于python实现名片管理系统,包括了基于python实现名片管理系统的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python实现名片管理系统的具体代码,供大家参考,具体内容如下 主程序: 程序工具包: 实现效果: 更多学习资料请关注专题《管理系统开发》。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍Java实现学生管理系统,包括了Java实现学生管理系统的使用技巧和注意事项,需要的朋友参考一下 项目描述:通过管理员帐号登录,对学员信息进行管理。可以实现学员信息的增加、修改、删除、查询。 知识点:数组、do{}while循环、for循环、if语句、switch条件语句 学生管理系统的流程图 以下为优化后的代码 本代码为Java初级人员编写,方法运用不是很恰当,仅供娱乐。 以上就是

  • 本文向大家介绍Android实现学生管理系统,包括了Android实现学生管理系统的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Android实现学生管理系统的关键性代码,供大家参考,具体内容如下 局部效果图:   实现代码: 1、布局 2、关键代码: 以上就是本文的全部内容,希望对大家的学习有所帮助。

  • 本文向大家介绍python初学者,用python实现基本的学生管理系统(python3)代码实例,包括了python初学者,用python实现基本的学生管理系统(python3)代码实例的使用技巧和注意事项,需要的朋友参考一下 这个是用python实现的基本的增删改查的学生管理系统吧,其中主要是对输入的数据进行合法性检测的问题,这次又对函数进行了练习!掌握函数更加熟练了!二话不说先贴代码,一切问题