python mysql實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
這學(xué)期在學(xué)python,感覺(jué)想寫一個(gè)東西來(lái)鞏固自己的基礎(chǔ),因?yàn)榇蠖臅r(shí)候我看過(guò)python,所以還是一共花了幾個(gè)小時(shí)寫了一個(gè)基于mysql的成績(jī)管理系統(tǒng),這個(gè)東西其實(shí)拿不出手,不過(guò)就當(dāng)復(fù)習(xí)基本了。
1 、首先如果你python中沒(méi)安裝mysql的驅(qū)動(dòng),還是要打開(kāi)cmd命令行安裝一下才可以使用:
pip3 install PyMySQL
2 、創(chuàng)建數(shù)據(jù)庫(kù)studentdb,你可以在圖形化界面sqlyog中創(chuàng)建:
3 、然后在數(shù)據(jù)庫(kù)中創(chuàng)建表st
4 、python連接數(shù)據(jù)庫(kù)的核心代碼:
db = pymysql.connect(host='localhost', user='root', password='333', database='studentdb') cursor = db.cursor()
user就是你自己設(shè)置的用戶名,password就是你自己設(shè)置的密碼,database就是你剛剛設(shè)置的數(shù)據(jù)庫(kù)
5 、另外你也可以選擇mysql命令行創(chuàng)建數(shù)據(jù)庫(kù)以及數(shù)據(jù)表都是可以的
6 、完整代碼如下:
import pymysql def menu(): print("\t\t學(xué)生信息管理系統(tǒng)\t\t") print("\t\t1 添加學(xué)生\t\t") print("\t\t2 刪除學(xué)生\t\t") print("\t\t3 修改學(xué)生\t\t") print("\t\t4 按成績(jī)降序排序:\t\t") print("\t\t5 按成績(jī)升序排序:\t\t") print("\t\t6 查詢:\t\t") print("\t\t7 退出程序\t\t") pass def insert(): print("插入學(xué)生信息") name = input("請(qǐng)輸入學(xué)生姓名:") sex = input("請(qǐng)輸入學(xué)生性別:") snum = input("請(qǐng)輸入學(xué)生學(xué)號(hào):") chinese = int(input("請(qǐng)輸入學(xué)生語(yǔ)文成績(jī):")) math = int(input("請(qǐng)輸入學(xué)生數(shù)學(xué)成績(jī):")) stotal = int(input("請(qǐng)輸入學(xué)生總成績(jī):")) db = pymysql.connect(host='localhost', user='root', password='333', database='studentdb') cursor = db.cursor() sql = "insert into st(sname,ssex,snum,chinese,math,stotal) values('%s','%s','%s',%d,%d,%d)"%(name,sex,snum,chinese,math,stotal) try: cursor.execute(sql) db.commit() except: db.rollback() print("error") db.close() pass def delete(): while 1: print("刪除學(xué)生信息") choose1 = int(input("請(qǐng)輸入您的刪除方式:1 按姓名 2 按學(xué)號(hào): ")) if choose1 == 1: print("按姓名刪除") strname = input("請(qǐng)輸入刪除學(xué)生的名字:") db = pymysql.connect(host='localhost', user='root', password='333', database='studentdb') cursor = db.cursor() sql1 = "select *from st where sname='%s'"%(strname) cursor.execute(sql1) if cursor.rowcount>0: try: sql = "DELETE FROM st WHERE sname = '%s'" % (strname) cursor.execute(sql) db.commit() print("刪除姓名為%s的學(xué)生信息成功" % (strname)) break except: db.rollback() print("error") db.close() else: print("沒(méi)有這個(gè)學(xué)生的信息,輸入錯(cuò)誤") break elif choose1 == 2: print("按學(xué)號(hào)刪除") strnum = input("請(qǐng)輸入刪除學(xué)生的學(xué)號(hào):") db = pymysql.connect(host='localhost', user='root', password='333', database='studentdb') cursor = db.cursor() sql1 = "select *from st where snum='%s'"%(strnum) cursor.execute(sql1) if cursor.rowcount>0: sql = "DELETE FROM st WHERE snum = '%s'" % (strnum) try: cursor.execute(sql) db.commit() print("刪除學(xué)號(hào)為%s學(xué)生的信息成功" % (strnum)) break except: db.rollback() print("error") db.close() else: print("沒(méi)有這個(gè)學(xué)生的信息,輸入錯(cuò)誤") break else: print("輸入錯(cuò)誤,重新輸入") break pass pass pass def change(): while 1: print("改變學(xué)生信息") myname = input("請(qǐng)輸入改變學(xué)生的名字:") db = pymysql.connect(host='localhost', user='root', password='333', database='studentdb') cursor = db.cursor() sql1 = "select *from st where sname='%s'"%(myname) cursor.execute(sql1) results = cursor.fetchall() name12='0' for row in results: name12 = row[0] if myname == name12: try: sex = input("請(qǐng)輸入學(xué)生性別:") snum = input("請(qǐng)輸入學(xué)生學(xué)號(hào):") chinese = int(input("請(qǐng)輸入學(xué)生語(yǔ)文成績(jī):")) math = int(input("請(qǐng)輸入學(xué)生數(shù)學(xué)成績(jī):")) stotal = int(input("請(qǐng)輸入學(xué)生總成績(jī):")) #有問(wèn)題解決不了 sql3 = "update st set ssex ='%s',snum='%s',chinese=%d,math=%d,stotal=%d where sname='%s'"%(sex,snum,chinese,math,stotal,name12) cursor.execute(sql3) db.commit() print("修改姓名為%s學(xué)生的信息成功"%(myname)) break except: db.rollback() print("error") db.close() break else: print("沒(méi)有這個(gè)學(xué)生的信息,輸入錯(cuò)誤") break pass pass pass def sortbyscoredesc(): print("按成績(jī)降序") db = pymysql.connect(host='localhost', user='root', password='333', database='studentdb') cursor = db.cursor() sql = "select * from st order by stotal desc" try: cursor.execute(sql) results = cursor.fetchall() for row in results: name1 = row[0] sex1 = row[1] num1 = row[2] chinses1 = row[3] math1 = row[4] total1 = row[5] # 打印結(jié)果 print("學(xué)生姓名:%s\t,學(xué)生性別:%s\t,學(xué)生學(xué)號(hào):%s\t,語(yǔ)文成績(jī):%d\t,數(shù)學(xué)成績(jī):%d\t,總成績(jī):%d\t" % \ (name1, sex1, num1, chinses1, math1, total1)) db.commit() except: db.rollback() print("error") db.close() pass def sortbyscore(): print("按成績(jī)升序排序") db = pymysql.connect(host='localhost', user='root', password='333', database='studentdb') cursor = db.cursor() sql = "select * from st order by stotal asc " try: cursor.execute(sql) results = cursor.fetchall() for row in results: name1 = row[0] sex1 = row[1] num1 = row[2] chinses1 = row[3] math1 = row[4] total1 = row[5] # 打印結(jié)果 print("學(xué)生姓名:%s\t,學(xué)生性別:%s\t,學(xué)生學(xué)號(hào):%s\t,語(yǔ)文成績(jī):%d\t,數(shù)學(xué)成績(jī):%d\t,總成績(jī):%d\t" % \ (name1, sex1, num1, chinses1, math1, total1)) db.commit() except: db.rollback() print("error") db.close() pass def select(): print("查詢學(xué)生信息") db = pymysql.connect(host='localhost',user='root',password='333',database='studentdb') cursor = db.cursor() sql = "select * from st" try: cursor.execute(sql) results = cursor.fetchall() for row in results: name1 = row[0] sex1 = row[1] num1 = row[2] chinses1 = row[3] math1 = row[4] total1 = row[5] # 打印結(jié)果 print("學(xué)生姓名:%s\t,學(xué)生性別:%s\t,學(xué)生學(xué)號(hào):%s\t,語(yǔ)文成績(jī):%d\t,數(shù)學(xué)成績(jī):%d\t,總成績(jī):%d\t" % \ (name1,sex1,num1,chinses1,math1,total1)) db.commit() except: db.rollback() print("error") db.close() pass def exitexe(): while 1: print("確定要退出系統(tǒng)嗎?退出(y) 不退出(n)") c = input("請(qǐng)輸入您的選擇aaaa:\t") if c == 'y': exit() elif c == 'n': break else: print("輸入有誤 重新輸入") pass pass if __name__ == '__main__': while True: menu() choose = int(input("請(qǐng)輸入您的選擇:\t")) if choose == 1: insert() elif choose == 2: delete() elif choose == 3: change() elif choose == 4: sortbyscoredesc() elif choose == 5: sortbyscore() elif choose == 6: select() elif choose == 7: exitexe() pass pass
7 、運(yùn)行結(jié)果如下:
8 、總結(jié):
在這個(gè)案例中,我遇到的問(wèn)題就是在sql語(yǔ)句和python間,有時(shí)候忘記commit,就要測(cè)試十幾分鐘來(lái)解決,然后就是如何設(shè)置用戶輸入錯(cuò)誤指令的問(wèn)題,我用了while循環(huán)和break pass來(lái)解決,總體上這個(gè)項(xiàng)目不難,對(duì)初學(xué)python的人很有用,我僅用了最基本的語(yǔ)法,元組,列表集合基本沒(méi)用,如果你有更好的想法私信我,下一步我將它開(kāi)發(fā)為一個(gè)圖形化界面的系統(tǒng)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用windows設(shè)置定時(shí)執(zhí)行腳本
這篇文章主要介紹了Python使用windows設(shè)置定時(shí)執(zhí)行腳本,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Python+Pygame實(shí)戰(zhàn)之文字劇情游戲的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何利用Python和Pygame實(shí)現(xiàn)兩款文字劇情游戲——《巨龍之洞》和《太空礦工》,感興趣的小伙伴可以了解一下2022-12-12python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5表單布局控件QFormLayout詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QFormLayout詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-03-03Python 帶你快速上手 Apache APISIX 插件開(kāi)發(fā)
Apache APISIX Python Runner 來(lái)了,社區(qū)中的小伙伴們?cè)陂_(kāi)發(fā) Apache APISIX 插件時(shí)又多了一種新選擇,本文將用實(shí)列向大家介紹,需要的朋友可以參考下面文章內(nèi)容2021-09-09Python中利用all()來(lái)優(yōu)化減少判斷的實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于Python中利用all()來(lái)優(yōu)化減少判斷的實(shí)例分析內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-06-06python+opencv+caffe+攝像頭做目標(biāo)檢測(cè)的實(shí)例代碼
今天小編就為大家分享一篇python+opencv+caffe+攝像頭做目標(biāo)檢測(cè)的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08python實(shí)現(xiàn)微信自動(dòng)回復(fù)及批量添加好友功能
這篇文章主要介紹了python實(shí)現(xiàn)微信自動(dòng)回復(fù)及python 批量生成微信添加好友截圖功能的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07