Python基于mysql實(shí)現(xiàn)學(xué)生管理系統(tǒng)
更新時(shí)間:2021年08月12日 14:38:23 作者:qd_tudou
這篇文章主要為大家詳細(xì)介紹了Python基于mysql實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本篇文章主要介紹了Python基于mysql實(shí)現(xiàn)學(xué)生管理系統(tǒng),分享給大家,具體如下:
import pymysql
import re
def idinput(string):
ID = input(string)
pattern = re.compile("^\d{1,3}$")
while not re.match(pattern, ID):
ID = input("請(qǐng)輸入1-3位整數(shù):")
return ID
def appendStudentInfo():
ID =idinput("請(qǐng)輸入學(xué)生學(xué)號(hào):")
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("該學(xué)號(hào)已存在,請(qǐng)重新輸入:")
sql = "select * from StuSys where ID = '%d'" % int(ID)
cursor.execute(sql)
name=input("請(qǐng)輸入學(xué)生姓名:")
chinese=input("請(qǐng)輸入語(yǔ)文成績(jī):")
while not chinese.isdigit() or int(chinese)>100 or int(chinese)<0:
chinese = input("輸入錯(cuò)誤,請(qǐng)重新輸入:")
math =input("請(qǐng)輸入數(shù)學(xué)成績(jī):")
while not math.isdigit() or int(math) > 100 or int(math) < 0:
math = input("輸入錯(cuò)誤,請(qǐng)重新輸入:")
english=input("請(qǐng)輸入英語(yǔ)成績(jī):")
while not english.isdigit() or int(english) > 100 or int(english) < 0:
english = input("輸入錯(cuò)誤,請(qǐng)重新輸入:")
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("請(qǐng)輸入要?jiǎng)h除的學(xué)生學(xué)號(hào):")
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("輸入錯(cuò)誤")
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("不存在該學(xué)生信息")
return False
else:
print("該學(xué)生信息如下:")
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("請(qǐng)輸入要的學(xué)生學(xué)號(hào):")
if querystudent(modifyid):
name = input("請(qǐng)重新輸入學(xué)生姓名:")
chinese = input("請(qǐng)重新輸入語(yǔ)文成績(jī):")
while not chinese.isdigit() or int(chinese) > 100 or int(chinese) < 0:
chinese = input("輸入錯(cuò)誤,請(qǐng)重新輸入:")
math = input("請(qǐng)重新輸入數(shù)學(xué)成績(jī):")
while not math.isdigit() or int(math) > 100 or int(math) < 0:
math = input("輸入錯(cuò)誤,請(qǐng)重新輸入:")
english = input("請(qǐng)重新輸入英語(yǔ)成績(jī):")
while not english.isdigit() or int(english) > 100 or int(english) < 0:
english = input("輸入錯(cuò)誤,請(qǐng)重新輸入:")
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]
# 打印結(jié)果
print("ID=%d,NAME=%s,CHINESE=%d,ENGLISH=%d,MATH=%d,TOTAL=%d" % \
(ID, NAME, CHINESE, ENGLISH, MATH,TOTAL))
def studentMenu():
print("="*30)
print("學(xué)生管理系統(tǒng)")
print("1、添加學(xué)生信息")
print("2、刪除學(xué)生信息")
print("3、查詢學(xué)生信息")
print("4、修改學(xué)生信息")
print("5、全部學(xué)生信息")
print("6、退出")
print("="*30)
if __name__ == '__main__':
while True:
studentMenu()
menuindex = input("請(qǐng)輸入選項(xiàng)序號(hào):")
while not menuindex.isdigit():
menuindex = input("輸入錯(cuò)誤,請(qǐng)重新輸入:")
if int(menuindex) ==1:
appendStudentInfo()
elif int(menuindex) ==2:
delstudent()
elif int(menuindex) ==3:
querystudentid = idinput("請(qǐng)輸入要查詢的學(xué)生學(xué)號(hào):")
querystudent(querystudentid)
elif int(menuindex) ==4:
modifystudentifo()
elif int(menuindex) == 5:
allinfo()
elif int(menuindex) == 6:
break
else:
print("輸入序號(hào)無(wú)效")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python學(xué)生信息管理系統(tǒng)(完整版)
- Python實(shí)現(xiàn)GUI學(xué)生信息管理系統(tǒng)
- python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
- python實(shí)現(xiàn)簡(jiǎn)易學(xué)生信息管理系統(tǒng)
- Python學(xué)生成績(jī)管理系統(tǒng)簡(jiǎn)潔版
- python學(xué)生管理系統(tǒng)代碼實(shí)現(xiàn)
- python學(xué)生信息管理系統(tǒng)
- python學(xué)生信息管理系統(tǒng)實(shí)現(xiàn)代碼
- python學(xué)生信息管理系統(tǒng)(初級(jí)版)
- python實(shí)現(xiàn)班級(jí)檔案管理系統(tǒng)
相關(guān)文章
python抓取京東價(jià)格分析京東商品價(jià)格走勢(shì)
本文介紹使用python抓取京東價(jià)格的代碼,用于分析京東商品價(jià)格走勢(shì)或者用于其它,大家參考使用吧2014-01-01
利用Python如何批量修改數(shù)據(jù)庫(kù)執(zhí)行Sql文件
這篇文章主要給大家介紹了關(guān)于利用Python如何批量修改數(shù)據(jù)庫(kù)執(zhí)行Sql文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
python實(shí)現(xiàn)局域網(wǎng)內(nèi)實(shí)時(shí)通信代碼
今天小編就為大家分享一篇python實(shí)現(xiàn)局域網(wǎng)內(nèi)實(shí)時(shí)通信代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
python 如何將帶小數(shù)的浮點(diǎn)型字符串轉(zhuǎn)換為整數(shù)
在python中如何實(shí)現(xiàn)將帶小數(shù)的浮點(diǎn)型字符串轉(zhuǎn)換為整數(shù)呢?今天小編就為大家介紹一下解決方案,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05

