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