您好,登錄后才能下訂單哦!
怎么在Python中使用mysql實(shí)現(xiàn)一個(gè)學(xué)生管理系統(tǒng)?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
Python是一種跨平臺(tái)的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xià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)輸入語文成績(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)輸入英語成績(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)重新輸入語文成績(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)重新輸入英語成績(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)無效")
關(guān)于怎么在Python中使用mysql實(shí)現(xiàn)一個(gè)學(xué)生管理系統(tǒng)問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。