Python基于mysql实现学生管理系统
作者:qd_tudou 发布时间:2024-01-24 05:57:47
标签: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("输入序号无效")
来源:https://blog.csdn.net/yaoliuwei1426/article/details/81101333


猜你喜欢
- 前言在一个分布式环境中,每台机器上可能需要启动和停止多个进程,使用命令行方式一个一个手动启动和停止非常麻烦,而且查看每个进程的状态也很不方便
- 之前使用email模块+smtplib模块发送邮件,虽然可以实现功能,但过程比较繁琐,今天发现一个宝藏库(yagmail),可以说是炒鸡好用
- 本文实例讲述了Python3.5多进程原理与用法。分享给大家供大家参考,具体如下:进程类:Process示例及代码:(1)创建函数作为单进程
- 在爬虫百度地图的期间,就为它做了一个界面,运用的是PyQt5。得到意想不到的结果:# -*- coding: utf-8 -*-# Form
- 1. 错误描述之前在学习Python的过程中,导入自己写的包文件时,与之相关的方法等都会被划红线,但并不影响代码执行,如图:看着红线确实有点
- conn.execute、rs.open之间的差别,conn.execute、rs.open、command.execute方法用法大大不同
- 增加索引可以提高查询效率。增加索引就是增加一个索引文件,存放的是数据的地址,类似与我们文档的目录,在查找过程中可以不用从书的内容查找,直接根
- 最近在做Python 的项目,特地整理了下 Python 序列的方法。序列se
- 前言个人一直觉得对学习任何知识而言,概念是相当重要的。掌握了概念和原理,细节可以留给实践去推敲。掌握的关键在于理解,通过具体的实例和实际操作
- 前言:线性回归模型属于经典的统计学模型,该模型的应用场景是根据已知的变量(即自变量)来预测某个连续的数值变量(即因变量)。例如餐厅根据媒体的
- 首先让我祭出一张数学王子高斯的照片,这位印在德国马克上的神人有多牛呢? 他是近代数学的奠基人之一,与牛顿, 阿基米德并称顶级三大数学家,随便
- python的版本经过了python2.x和python3.x等版本,无论哪种版本,关于python爬虫相关的知识是融会贯通的,脚本之家关于
- 需求:两个文件,一个文件为统计报表,里面含有手机号,另一个文件为手机号段归属地,含有手机号码前七位对应的地区。需要对统计报表进行处理,将手机
- Go mod开启 Go Modulego env -w GO111MODULE=on或set GO111MODULE=on设置Go Prox
- 使用现有的txt文本和图片,就可以用wordcloud包生成词云图。大致步骤是:1、读取txt文本并简单处理;2、读取图片,以用作背景;3、
- 本文实例为大家分享了python抖音表白程序的具体代码,供大家参考,具体内容如下import sysimport randomimport
- 开始一个组件,毫无目的的写代码是一个不好的习惯,要经历 分析 => 抽象 => 实现 => 应用 四个阶段。组件DEMO地
- 基于上一篇文章,这篇文章是关于使用coverage来实现代码覆盖的操作实例,源代码在上一篇已经给出相应链接。本篇文章字用来实现代码覆盖的源代
- 我们大家都知道MySQL数据库在安装完之后,默认的MySQL数据库,其最大连接数为100,一般流量稍微大一点的论坛或网站这个连接数是远远不够
- 安装Pycharm进行Python开发时,经常右下角提示No R interpreter defined,处理方式:1、安装R,然后将R的路