Python基于mysql实现学生管理系统
本篇文章主要介绍了Python基于mysql实现学生管理系统,分享给大家,具体如下:
importpymysql
importre
defidinput(string):
ID=input(string)
pattern=re.compile("^\d{1,3}$")
whilenotre.match(pattern,ID):
ID=input("请输入1-3位整数:")
returnID
defappendStudentInfo():
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*fromStuSyswhereID='%s'"%ID
cursor.execute(sql)
whilecursor.rowcount>0:
ID=idinput("该学号已存在,请重新输入:")
sql="select*fromStuSyswhereID='%d'"%int(ID)
cursor.execute(sql)
name=input("请输入学生姓名:")
chinese=input("请输入语文成绩:")
whilenotchinese.isdigit()orint(chinese)>100orint(chinese)<0:
chinese=input("输入错误,请重新输入:")
math=input("请输入数学成绩:")
whilenotmath.isdigit()orint(math)>100orint(math)<0:
math=input("输入错误,请重新输入:")
english=input("请输入英语成绩:")
whilenotenglish.isdigit()orint(english)>100orint(english)<0:
english=input("输入错误,请重新输入:")
total=int(chinese)+int(math)+int(english)
sql="""INSERTINTOStuSys(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()
defdelstudent():
delstudentid=idinput("请输入要删除的学生学号:")
ifquerystudent(delstudentid):
select=input("是否删除:是(Y)/否(N)")
ifselect=="Y"orselect=="y":
db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
cursor=db.cursor()
sql="deletefromstusyswhereID=%s"%delstudentid
cursor.execute(sql)
db.commit()
db.close()
print("删除成功")
elifselect=="N"orselect=="n":
print("取消删除")
else:
print("输入错误")
defquerystudent(querystudentid):
db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
cursor=db.cursor()
sql="select*fromstusyswhereID=%s"%querystudentid
cursor.execute(sql)
ifcursor.rowcount==0:
print("不存在该学生信息")
returnFalse
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]))
returnTrue
defmodifystudentifo():
modifyid=idinput("请输入要的学生学号:")
ifquerystudent(modifyid):
name=input("请重新输入学生姓名:")
chinese=input("请重新输入语文成绩:")
whilenotchinese.isdigit()orint(chinese)>100orint(chinese)<0:
chinese=input("输入错误,请重新输入:")
math=input("请重新输入数学成绩:")
whilenotmath.isdigit()orint(math)>100orint(math)<0:
math=input("输入错误,请重新输入:")
english=input("请重新输入英语成绩:")
whilenotenglish.isdigit()orint(english)>100orint(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="updatestusyssetname='%s'whereid=%s"%(name,modifyid)
cursor.execute(sql1)
sql2="updatestusyssetmath=%swhereid=%s"%(math,modifyid)
cursor.execute(sql2)
sql3="updatestusyssetenglish=%swhereid=%s"%(english,modifyid)
cursor.execute(sql3)
sql4="updatestusyssettotal=%swhereid=%s"%(total,modifyid)
cursor.execute(sql4)
sql5="updatestusyssetchinese=%swhereid=%s"%(chinese,modifyid)
cursor.execute(sql5)
db.commit()
db.close()
defallinfo():
db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
cursor=db.cursor()
sql="select*fromstusys"
cursor.execute(sql)
results=cursor.fetchall()
forrowinresults:
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))
defstudentMenu():
print("="*30)
print("学生管理系统")
print("1、添加学生信息")
print("2、删除学生信息")
print("3、查询学生信息")
print("4、修改学生信息")
print("5、全部学生信息")
print("6、退出")
print("="*30)
if__name__=='__main__':
whileTrue:
studentMenu()
menuindex=input("请输入选项序号:")
whilenotmenuindex.isdigit():
menuindex=input("输入错误,请重新输入:")
ifint(menuindex)==1:
appendStudentInfo()
elifint(menuindex)==2:
delstudent()
elifint(menuindex)==3:
querystudentid=idinput("请输入要查询的学生学号:")
querystudent(querystudentid)
elifint(menuindex)==4:
modifystudentifo()
elifint(menuindex)==5:
allinfo()
elifint(menuindex)==6:
break
else:
print("输入序号无效")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。