實(shí)現(xiàn)學(xué)生選課系統(tǒng)
Python3.6
pymysql(Python連接MySQL)
xlrd(操作Excel)
1.首先運(yùn)行First_run.py:
功能:創(chuàng)建數(shù)據(jù)庫(kù)、表等信息
2.運(yùn)行seconnd_run.py:
功能: 實(shí)現(xiàn)學(xué)生選課
3.賬號(hào)密碼.xlsx:
存放學(xué)生信息(可以存班級(jí)花名冊(cè))
如:
``
表之間的聯(lián)系
student_login:存放學(xué)生賬號(hào)信息(直接導(dǎo)入班級(jí)花名冊(cè),具體看代碼) 字段: s_no:學(xué)生學(xué)號(hào), s_name:學(xué)生姓名, s_login:學(xué)生賬號(hào), s_pd:學(xué)生密碼 course:存放課程信息 字段: c_id:課程編號(hào) c_name:課程名稱(chēng) student_class:學(xué)生選課表,存放學(xué)生選課信息 字段: s_no:學(xué)生學(xué)號(hào)(設(shè)置外鍵與student_login表s_no連接) c_id:課程編號(hào)(設(shè)置外鍵與course表c_id連接) admin_login:管理員信息表,存放管理員賬號(hào) 字段: a_no: 管理員編號(hào) a_name: 管理員姓名 a_login: 管理員賬號(hào) a_pd: 管理員密碼
First_run.py代碼如下:
import pymysql import xlrd def create_all(): try: password = input('請(qǐng)輸入mysql密碼(root用戶):') db = pymysql.connect(host='localhost', user='root', password=password) cursor = db.cursor() except pymysql.err.OperationalError: print('密碼輸入錯(cuò)誤!') else: try: sql = 'create database student charset utf8;' cursor.execute(sql) except pymysql.err.ProgrammingError: print("Can't create database 'student' database exists!") else: sql0 = 'use student;' # 創(chuàng)建課程表 sql1 = "CREATE TABLE course (c_id int(10) PRIMARY KEY AUTO_INCREMENT, c_name VARCHAR ( 30 ) NOT NULL)default charset utf8;" # 創(chuàng)建學(xué)生賬號(hào)表 sql2 = "create table student_login(s_no char(10), s_name varchar(30), s_login char(20), s_pd char(20) not null, primary key(s_no)) default charset utf8;" # 創(chuàng)建學(xué)生選課表 sql3 = "CREATE TABLE student_class (s_no CHAR(10),c_id INT,CONSTRAINT FOREIGN KEY (s_no) REFERENCES student_login (s_no),CONSTRAINT FOREIGN KEY (c_id) REFERENCES course (c_id),unique(s_no,c_id)) default charset utf8;" # unique(s_no,c_id))聯(lián)合唯一,確保選課唯一 # 創(chuàng)建管理員賬號(hào)表 sql4 = "create table admin_login(a_no char(10), a_name varchar(30), a_login char(10) unique, a_pd char(10) not null, primary key(a_no)) default charset utf8;" cursor.execute(sql0) cursor.execute(sql1) cursor.execute(sql2) cursor.execute(sql3) cursor.execute(sql4) db.commit() print('Successful!') def insert_student_login(db): def open_excel(): try: book = xlrd.open_workbook("賬號(hào)密碼.xlsx") # 文件名,把文件與py文件放在同一目錄下 except: print("Open excel file failed!") else: try: sheet = book.sheet_by_name("Sheet1") # execl里面的sheet1 except: print('No Sheet1') else: print('Yes') return sheet def insert_data(): sheet = open_excel() cursor = db.cursor() for i in range(1, sheet.nrows): # 第一行是標(biāo)題名,對(duì)應(yīng)表中的字段名所以應(yīng)該從第二行開(kāi)始,計(jì)算機(jī)以0開(kāi)始計(jì)數(shù),所以值是1 s_no = str(sheet.cell(i, 0).value)[0:10] # 取第i行第0列 s_name = sheet.cell(i, 1).value # 取第i行第1列,下面依次類(lèi)推 s_login = str(sheet.cell(i, 2).value)[0:10] s_pd = str(sheet.cell(i, 3).value)[0:10] # print(name) # print(data) # value = (name,data) # print(value) sql = "INSERT INTO student_login VALUES('%s','%s','%s','%s')" % (s_no, s_name, s_login, s_pd) cursor.execute(sql) # 執(zhí)行sql語(yǔ)句 db.commit() insert_data() # cursor.close() # 關(guān)閉連接 # db.close()#關(guān)閉數(shù)據(jù) print("插入成功!") def insert_admin_login(db): try: cursor = db.cursor() sql = 'insert into admin_login values("1","admin","1","1")' cursor.execute(sql) db.commit() except: print('Insert admin_login Failed?。?!') else: print('Successful!') def insert_into_course(db): try: cursor = db.cursor() sql = 'insert into course values(1,"高數(shù)"),(2,"大學(xué)英語(yǔ)");' # 默認(rèn)插入兩個(gè)課程供選擇 cursor.execute(sql) db.commit() except: print('Insert course Failed!') else: print('Successful!') def main(): create_all() try: passwd = input('請(qǐng)輸入MySQL密碼:') db = pymysql.connect(host="localhost", user="root", passwd=passwd, db="student", charset='utf8') except: print("Could not connect to mysql server!") else: insert_student_login(db) insert_admin_login(db) insert_into_course(db) if __name__ == '__main__': main()
second_run.py代碼如下:
import pymysql # 創(chuàng)建游標(biāo)函數(shù) def get_db(): try: passwd = input('請(qǐng)輸入MySQL密碼:') db = pymysql.connect('127.0.0.1', 'root', passwd, 'student') except pymysql.err.OperationalError: print('密碼輸入錯(cuò)誤!Go Die!') else: return db def get_cursor(db): cursor = db.cursor() return cursor # 選擇身份 def login(db, cursor): menu_login() i = 0 while True: i += 1 # 設(shè)置循環(huán),超過(guò)三次退出系統(tǒng) login_select = input('請(qǐng)輸入你的選項(xiàng):') if login_select == '1': # 這里數(shù)字為字符串類(lèi)型,記得要引號(hào)! student_login(db, cursor) # 跳入學(xué)生登錄頁(yè)面 elif login_select == '2': admin_login(db, cursor) # 跳入管理員登錄頁(yè)面 else: print('請(qǐng)輸入正確的選項(xiàng)!>>>>>>>>請(qǐng)擦擦眼睛再重選--*--(^_^)--*-- :') if i >= 3: print('GoodBye了您??!') break # 學(xué)生登錄認(rèn)證 def student_login(db,cursor): print(' -----------------****----------^_^|-歡迎進(jìn)入學(xué)生系統(tǒng)-|^_^----------****---------------------------- ') l = 0 while True: login = input('請(qǐng)輸入你的賬號(hào):') sql = "SELECT * FROM student_login where student_login.s_login='%s'" % login cursor.execute(sql) login_id = cursor.fetchall() if len(login_id) == 0: l += 1 print('賬號(hào)不存在,請(qǐng)重新輸入:') if l >= 3: print() print('賬號(hào)不存在,請(qǐng)聯(lián)系管理員申請(qǐng)賬號(hào)!') exit() else: p = 0 # 第一次錯(cuò)誤:放在了while語(yǔ)句里面,導(dǎo)致超過(guò)三次無(wú)法退出(每次循環(huán)都會(huì)將p初始化為0) while True: password = input('請(qǐng)輸入你的密碼:') sql2 = "SELECT * FROM student_login where student_login.s_login='%s'and student_login.s_pd ='%s'" % ( login, password) cursor.execute(sql2) login_pd = cursor.fetchall() if len(login_pd) == 0: p += 1 print('密碼錯(cuò)誤!') if p >= 3: print('密碼輸入錯(cuò)誤三次,GoodBye了您?。?) exit() elif len(login_pd) != 0: sql3 = "SELECT s_name,s_no from student_login where s_login = '%s'; " % login # sql4 = "select s_no from student_login where s_login = '%s';" % login cursor.execute(sql3) # cursor.execute(sql4) data = cursor.fetchall()[0] s_name = data[0] # 姓名 s_no = data[1] # 學(xué)號(hào) print() print(" -------------****----------^_^歡迎--|", s_name, "|--進(jìn)入學(xué)生選課系統(tǒng)^_^----------****-----------------") # 學(xué)生系統(tǒng)模塊 i = 0 while True: student_select_menu() student_select = input('請(qǐng)輸入你的選項(xiàng):') if student_select == '1': show_course(cursor) elif student_select == '2': select_course(db, cursor, s_name, s_no) elif student_select == '3': show_class(cursor, s_no) # exit() elif student_select == '4': update_class(db, cursor, s_name, s_no) elif student_select == '5': print('\n您已退出登錄^_^\n') select = input('請(qǐng)輸入 1 或 2 分別進(jìn)入學(xué)生與管理員系統(tǒng) or 輸入 0 退出系統(tǒng):') if select == '1': student_login(db, cursor) elif select == '2': admin_login(db, cursor) elif select == '0': exit() else: print('請(qǐng)輸入正確選項(xiàng)!') elif i >= 3: print('GoodBye了您啊!') print() break # 重新登錄學(xué)生操作,密碼鎖定 else: i += 1 print('請(qǐng)輸入正確的選項(xiàng)!>>>>>>>>請(qǐng)擦擦眼睛再重選--*--(^_^)--*-- :') # 管理員登錄認(rèn)證 def admin_login(db, cursor): print(' -------------------****----------^_^|-歡迎進(jìn)入管理員系統(tǒng)-|^_^----------****-------------------------- ') l = 0 while True: login = input('請(qǐng)輸入你的賬號(hào):') sql = "SELECT * FROM admin_login where admin_login.a_login='%s'" % login cursor.execute(sql) login_id = cursor.fetchall() if len(login_id) == 0: l += 1 print('賬號(hào)不存在,請(qǐng)重新輸入:') if l >= 3: print() print('賬號(hào)不存在,請(qǐng)聯(lián)系站長(zhǎng)申請(qǐng)賬號(hào)!') exit() else: p = 0 # 第一次錯(cuò)誤:放在了while語(yǔ)句里面,導(dǎo)致超過(guò)三次無(wú)法退出(每次循環(huán)都會(huì)將p初始化為0) while True: password = input('請(qǐng)輸入你的密碼:') sql2 = "SELECT * FROM admin_login where admin_login.a_login='%s'and admin_login.a_pd ='%s'" % ( login, password) cursor.execute(sql2) login_pd = cursor.fetchall() if len(login_pd) == 0: p += 1 print('密碼錯(cuò)誤!') if p >= 3: print('密碼輸入錯(cuò)誤三次,GoodBye了您?。?) exit() elif len(login_pd) != 0: sql3 = "SELECT a_name from admin_login where a_login = '%s'; " % login cursor.execute(sql3) s_name = cursor.fetchall()[0][0] print() print(" --------------****----------^_^歡迎--|", s_name, "|--進(jìn)入管理員系統(tǒng)^_^----------****-----------------") # 管理員系統(tǒng)模塊 i = 0 while True: admin_select_menu() admin_select = input('請(qǐng)輸入你的選項(xiàng):') if admin_select == '1': show_course(cursor) # exit() elif admin_select == '0': delete_course(db, cursor) elif admin_select == '2': add_course(db, cursor) # exit() elif admin_select == '3': show_studentlogin(cursor) # exit() elif admin_select == '4': add_studentlogin(db, cursor) # exit() elif admin_select == '5': show_adminlogin(cursor) # exit() elif admin_select == '6': add_admin_login(db, cursor) # exit() elif admin_select == '7': show_student_class(cursor) elif admin_select == '8': print('您已退出登錄!\n') select = input('請(qǐng)輸入 1 或 2 分別進(jìn)入學(xué)生與管理員系統(tǒng) or 輸入 0 退出系統(tǒng):') if select == '1': student_login(db,cursor) elif select == '2': admin_login(db, cursor) elif select == '0': exit() else: print('請(qǐng)輸入正確選項(xiàng)!') elif i >= 3: print('GoodBye了您啊!') print() break # 重新登錄管理員系統(tǒng)操作 else: i += 1 print('請(qǐng)輸入正確的選項(xiàng)!>>>>>>>>請(qǐng)擦擦眼睛再重選--*--(^_^)--*-- :') # 登錄菜單欄 def menu_login(): menu_login1 = ''' -----------------------------****----------(^_^)----------****------------------------------------ | 歡迎登錄學(xué)生選課系統(tǒng) | | 1、學(xué)生登錄 | | 2、管理員登錄 | | (請(qǐng)?jiān)诓藛芜x擇你的操作,選擇其他無(wú)效,超過(guò)三次自動(dòng)退出系統(tǒng)?。? | ''' print(menu_login1) # 學(xué)生選課菜單欄 def student_select_menu(): menu_login = ''' | 1、查看當(dāng)前可選課程 | | 2、選擇課程 | | 3、查看已選課程 | | 4、更改課程 | | 5、退出系統(tǒng) | | (請(qǐng)?jiān)诓藛芜x擇你的操作,選擇其他無(wú)效,超過(guò)三次自動(dòng)退出系統(tǒng)?。? | ''' print(menu_login) # 管理員操作菜單 def admin_select_menu(): menu_login = ''' | 0、刪除課程 | | 1、查看所有課程 | | 2、添加課程 | | 3、查看所有學(xué)生賬號(hào)信息 | | 4、添加學(xué)生賬號(hào) | | 5、查看所有管理員信息 | | 6、添加管理員賬號(hào) | | 7、查看所有學(xué)生選課信息 | | 8、退出系統(tǒng) | | (請(qǐng)?jiān)诓藛芜x擇你的操作,選擇其他無(wú)效,超過(guò)三次自動(dòng)退出系統(tǒng)?。? | ''' print(menu_login) # 學(xué)生系統(tǒng)模塊 # 查看所有課程 完 def show_course(cursor): sql = "select * from course;" cursor.execute(sql) data = cursor.fetchall() # print(type(data)) # 元組類(lèi)型 item = len(data) if item == 0: print('暫無(wú)課程信息!') else: print() # 換行 print(' 課程如下:') for i in range(item): course = data[i] select = { "編號(hào)": course[0], "課程": course[1] } print(' ', select) # 選課 完成 def select_course(db, cursor, s_name, s_no): print(s_name) try: number = int(input('請(qǐng)輸入你的課程編號(hào):')) sql = "SELECT c_name FROM course where c_id = %s" % number # 查找課程名字 cursor.execute(sql) course = cursor.fetchall()[0][0] except IndexError: print('沒(méi)有你要選的課程,請(qǐng)重選!') except ValueError: print('請(qǐng)正確輸入課程編號(hào)!') else: print('你選的課程為:', course) confirm = input('是否繼續(xù)(Y/N):') if confirm == 'Y' or confirm == 'y': try: sql_insert = "insert into student_class values('%s','%s');" % (s_no, number) # 插入課程 cursor.execute(sql_insert) db.commit() except: print("課程已存在或選課失??!") else: print('Successful!') else: print('Failed??!') # 查看已選課 def show_class(cursor, s_no): try: sql = 'SELECT c_name FROM student_class sc INNER JOIN course c ON sc.c_id = c.c_id INNER JOIN student_login sl ON sc.s_no = sl.s_no where sc.s_no = "%s";' % s_no cursor.execute(sql) data = cursor.fetchall() except IndexError: print('暫無(wú)選課信息!') else: print('\n 你選的課程為:') for i in range(len(data)): print(' ', data[i][0], '^_^') # 修改選課 def update_class(db, cursor, s_name, s_no): while True: try: course = input('請(qǐng)輸入你要修改的課程編號(hào):') sql0 = "select * from student_class where s_no = '%s' and c_id = '%s'" % (s_no, course) cursor.execute(sql0) data0 = cursor.fetchall() if len(data0) != 0: re_course = input('請(qǐng)輸入你要選的課程編號(hào):') sql = "select c_name from course where c_id = %s" % re_course # 查找是否存在課程編號(hào) cursor.execute(sql) data = cursor.fetchall() # 課程編號(hào)所對(duì)應(yīng)課程 else: print('你沒(méi)有選擇這個(gè)課程!') # 選課表中學(xué)生沒(méi)有選擇該課程 continue # 終止后面語(yǔ)句,進(jìn)入下次循環(huán) except IndexError: print('沒(méi)有你要選的課程,請(qǐng)重選!') else: if len(data) != 0: print('你重選的課程為:', data[0][0]) # data[0][0] 切片出來(lái)課程名稱(chēng) confirm = input('是否繼續(xù)(Y/y):') if confirm == 'Y' or confirm == 'y': try: sql = "UPDATE `student`.`student_class` SET `c_id` = '%s' WHERE `s_no` = '%s' AND `c_id` = '%s' LIMIT 1" % ( re_course, s_no, course) # 更新課程 cursor.execute(sql) db.commit() except: print("失敗") else: print('Successful!') break # 修改成功退出循環(huán) else: print('Failed?。?) else: print('沒(méi)有這個(gè)課程!') # 管理員模塊 # 添加課程 def delete_course(db, cursor): try: course = input('請(qǐng)輸入你要?jiǎng)h除的課程編號(hào):') sql = 'DELETE FROM course WHERE c_id = %s ' % course cursor.execute(sql) db.commit() except: print('刪除失敗!') else: print('刪除成功 ^_^') def add_course(db, cursor): course = input('請(qǐng)輸入你要插入的課程:') try: sql = "INSERT INTO course(c_name) VALUES ('%s')" % course cursor.execute(sql) db.commit() # 執(zhí)行插入語(yǔ)句 except pymysql.err.IntegrityError: print('課程已經(jīng)存在,不能重復(fù)!') else: print('添加成功') # 查看學(xué)生賬號(hào) (已完成) def show_studentlogin(cursor): sql = 'select * from student_login;' cursor.execute(sql) data = cursor.fetchall() print(' 學(xué)生賬號(hào)如下:\n') for i in range(len(data)): item = data[i] dict = { 'sno': item[0], 's_name': item[1], 's_login': item[2], 's_pd': item[3] } print(' ', dict) # 添加學(xué)生賬號(hào) def add_studentlogin(db, cursor): try: s_no = input('請(qǐng)輸入學(xué)生的學(xué)號(hào):') s_name = input('請(qǐng)輸入學(xué)生的姓名:') s_login = input('請(qǐng)輸入學(xué)生的賬號(hào):') s_pd = input('請(qǐng)輸入學(xué)生的密碼:') cursor.execute('insert into student_login values("%s","%s","%s","%s");'% (s_no, s_name, s_login, s_pd)) db.commit() except pymysql.err.IntegrityError: print('添加失敗,學(xué)號(hào)/賬號(hào)已存在!') else: print('添加成功^_^') # 查看管理員賬號(hào) 完 def show_adminlogin(cursor): sql = 'select * from admin_login;' cursor.execute(sql) data = cursor.fetchall() for i in range(len(data)): item = data[i] dict = { 'sno': item[0], 's_name': item[1], 's_login': item[2], 's_pd': item[3] } print(' ', dict) def add_admin_login(db, cursor): # 注意,傳入?yún)?shù)的時(shí)候一定要先傳db再傳游標(biāo) try: s_no = input('請(qǐng)輸入管理員的編號(hào):') s_name = input('請(qǐng)輸入管理員的姓名:') s_login = input('請(qǐng)輸入管理員的賬號(hào):') s_pd = input('請(qǐng)輸入管理員的密碼:') sql = 'insert into admin_login values("%s","%s","%s","%s");' % (s_no, s_name, s_login, s_pd) cursor.execute(sql) db.commit() except pymysql.err.IntegrityError: print('添加失敗,編號(hào)/賬號(hào)已存在!') else: print('添加成功^_^') # 查看學(xué)生選課信息 (完) def show_student_class(cursor): sql = 'SELECT * FROM student_class sc INNER JOIN course c ON sc.c_id = c.c_id INNER JOIN student_login sl ON sc.s_no = sl.s_no order by s_name;' cursor.execute(sql) data = cursor.fetchall() print('\n') if len(data) > 1: for i in range(len(data)): item = data[i] # print(item) # 打印查詢結(jié)果 dict = { # 取值 'sc_no': item[0], 'sc_name': item[5], 'sc_course': item[3] } print(' ', dict) else: print('沒(méi)有選課信息') def main(): try: db = get_db() cursor = get_cursor(db) except AttributeError: print('AttributeError!') else: login(db, cursor) if __name__ == '__main__': main()
運(yùn)行First_run:
這里因?yàn)槲乙呀?jīng)創(chuàng)建過(guò)數(shù)據(jù)庫(kù),try語(yǔ)句直接捕獲錯(cuò)誤
刪除student數(shù)據(jù)庫(kù)后重新運(yùn)行(亦可在SQL語(yǔ)句中加入判斷是否存在數(shù)據(jù)庫(kù))
這時(shí)可見(jiàn)我們的數(shù)據(jù)庫(kù)及表等信息已經(jīng)創(chuàng)建完成
可以看下數(shù)據(jù)庫(kù)確認(rèn)一下:
接著運(yùn)行second_run:
1.學(xué)生登錄
具體的功能請(qǐng)自行查看。
當(dāng)然代碼有很多不足的地方:
沒(méi)有封裝成類(lèi),全部代碼均為函數(shù)嵌套式的,層次不是特別鮮明
沒(méi)有可視化的界面,可以添加tkinter模塊增加有好的可視化界面。
到此這篇關(guān)于Python實(shí)戰(zhàn)之實(shí)現(xiàn)簡(jiǎn)易的學(xué)生選課系統(tǒng)的文章就介紹到這了,更多相關(guān)Python學(xué)生選課系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:山東 駐馬店 成都 蘭州 常州 六盤(pán)水 江蘇 宿遷
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python實(shí)戰(zhàn)之實(shí)現(xiàn)簡(jiǎn)易的學(xué)生選課系統(tǒng)》,本文關(guān)鍵詞 Python,實(shí)戰(zhàn),之,實(shí)現(xiàn),簡(jiǎn)易,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。