python mysql實現學生成績管理系統

這學期在學python,感覺想寫一個東西來鞏固自己的基礎,因為大二的時候我看過python,所以還是一共花瞭幾個小時寫瞭一個基於mysql的成績管理系統,這個東西其實拿不出手,不過就當復習基本瞭

1 首先如果你python中沒安裝mysql的驅動,還是要打開cmd命令行安裝一下才可以使用:

pip3 install PyMySQL

2 創建數據庫studentdb,你可以在圖形化界面sqlyog中創建:

3 然後在數據庫中創建表st

4 python連接數據庫的核心代碼:

db = pymysql.connect(host='localhost',
                         user='root',
                         password='333',
                         database='studentdb')
    cursor = db.cursor()

user就是你自己設置的用戶名,password就是你自己設置的密碼,database就是你剛剛設置的數據庫

5 另外你也可以選擇mysql命令行創建數據庫以及數據表都是可以的

6 完整代碼如下:

import pymysql
def menu():
    print("\t\t學生信息管理系統\t\t")
    print("\t\t1 添加學生\t\t")
    print("\t\t2 刪除學生\t\t")
    print("\t\t3 修改學生\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("插入學生信息")
    name = input("請輸入學生姓名:")
    sex = input("請輸入學生性別:")
    snum = input("請輸入學生學號:")
    chinese = int(input("請輸入學生語文成績:"))
    math = int(input("請輸入學生數學成績:"))
    stotal = int(input("請輸入學生總成績:"))
    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("刪除學生信息")
        choose1 = int(input("請輸入您的刪除方式:1 按姓名 2 按學號: "))
        if choose1 == 1:
            print("按姓名刪除")
            strname = input("請輸入刪除學生的名字:")
            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的學生信息成功" % (strname))
                    break
                except:
                    db.rollback()
                    print("error")
                db.close()
            else:
                print("沒有這個學生的信息,輸入錯誤")
                break
        elif choose1 == 2:
            print("按學號刪除")
            strnum = input("請輸入刪除學生的學號:")
            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("刪除學號為%s學生的信息成功" % (strnum))
                    break
                except:
                    db.rollback()
                    print("error")
                db.close()
            else:
                print("沒有這個學生的信息,輸入錯誤")
                break
        else:
            print("輸入錯誤,重新輸入")
            break
            pass
        pass
    pass


def change():
    while 1:
        print("改變學生信息")
        myname = input("請輸入改變學生的名字:")
        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("請輸入學生性別:")
                snum = input("請輸入學生學號:")
                chinese = int(input("請輸入學生語文成績:"))
                math = int(input("請輸入學生數學成績:"))
                stotal = int(input("請輸入學生總成績:"))
                #有問題解決不瞭
                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學生的信息成功"%(myname))
                break
            except:
                db.rollback()
                print("error")
            db.close()
            break
        else:
            print("沒有這個學生的信息,輸入錯誤")
            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]
            # 打印結果
            print("學生姓名:%s\t,學生性別:%s\t,學生學號:%s\t,語文成績:%d\t,數學成績:%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]
            # 打印結果
            print("學生姓名:%s\t,學生性別:%s\t,學生學號:%s\t,語文成績:%d\t,數學成績:%d\t,總成績:%d\t" % \
                  (name1, sex1, num1, chinses1, math1, total1))
        db.commit()
    except:
        db.rollback()
        print("error")
    db.close()
    pass
def select():
    print("查詢學生信息")
    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]
            # 打印結果
            print("學生姓名:%s\t,學生性別:%s\t,學生學號:%s\t,語文成績:%d\t,數學成績:%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("確定要退出系統嗎?退出(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 運行結果如下:

8 總結:

在這個案例中,我遇到的問題就是在sql語句和python間,有時候忘記commit,就要測試十幾分鐘來解決,然後就是如何設置用戶輸入錯誤指令的問題,我用瞭while循環和break pass來解決,總體上這個項目不難,對初學python的人很有用,我僅用瞭最基本的語法,元組,列表集合基本沒用,如果你有更好的想法私信我,下一步我將它開發為一個圖形化界面的系統。

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: