1. 程式人生 > >Python實現學生資訊管理系統(修改版)

Python實現學生資訊管理系統(修改版)

在學習之前先要了解sqlite遊標的使用方法python使用sqlite3時遊標的使用方法
繼上篇部落格Python實現學生資訊管理系統後,我就覺得寫的太複雜了,然後又是一通優化、優化、優化;
本次優化主要修改了:
1.使用遊標的方法連線、增、刪、改、查資料庫;
2.一般二級選單是不能直接退出程式的,所以去掉了二級選單退出程式的功能;
3.增加了連表查詢;
4.但是還有一點很不滿意,就是每次退出後都退出到主選單而不是當前選單,這點還沒改好,希望小夥伴能一起學習交流!

#-*- coding:utf-8 -*-
import sqlite3
#開啟本地資料庫用於儲存使用者資訊
cx = sqlite3.connect('student.db'
) #在該資料庫下建立學生資訊表 cx.execute ('''CREATE TABLE StudentTable( ID INTEGER PRIMARY KEY AUTOINCREMENT, StuId INTEGER NOT NULL, NAME TEXT NOT NULL, CLASS INT NOT NULL);''') print "Table created successfully"; #在該資料庫下建立課程資訊表 cx.execute ('''CREATE TABLE CourseTable( ID INTEGER PRIMARY KEY AUTOINCREMENT, CourseId INT NOT NULL, Name TEXT NOT NULL, Teacher TEXT NOT NULL, Classroom TEXT NOT NULL, StartTime CHAR(11) NOT NULL, EndTime CHAR(11) NOT NULL);'''
) print "Table created successfully"; #在該資料庫下建立選課情況資訊表 cx.execute ('''CREATE TABLE XuankeTable( ID INTEGER PRIMARY KEY AUTOINCREMENT, StuId INT NOT NULL, CourseId INT NOT NULL);''') print "Table created successfully"; #以上三個表建立完後,再次執行程式時,需要把三個建表程式碼註釋掉,否則會提示:該表已存在。即建表只需建一次。
def insert_stu():#錄入學生資訊 cu = cx.cursor() stu_id = input("請輸入學生學號:") cu.execute("SELECT StuId from StudentTable where StuId = '%s';"%stu_id) row = cu.fetchone() if row: print "sorry,該學號已存在,請重新輸入" else: stu_name = raw_input("請輸入學生姓名:") stu_class = input("請輸入學生班級:") sql1 = "INSERT INTO StudentTable(StuId,NAME,CLASS)" sql1 += " VALUES(%d,'%s',%d);"%(stu_id,stu_name,stu_class) cu.execute(sql1) cx.commit() print "恭喜你,學生錄入成功!" cu.close() def xuanke():#學生選課 cu = cx.cursor() stu_id = input('請輸入要選課的學生學號:') sql2 = "select StuId from StudentTable where StuId = %d;"%(stu_id) cu.execute(sql2) row = cu.fetchone() if row: sql3 = "select CourseId,Name,Teacher,Classroom, StartTime,EndTime from CourseTable" cu.execute(sql3) rows = cu.fetchall() for row in rows: print "CourseId = ", row[0] print "Name = ", row[1] print "Teacher = ", row[2] print "Classroom = ",row[3] print "StartTime = ",row[4] print "EndTime = ",row[5], "\n" cou_id = input("請輸入要選的課程號:") sql0 = "select CourseId from CourseTable where CourseId =%d;"%(cou_id) cu.execute(sql0) row = cu.fetchone() if row: sql = "select StuId CourseId from XuankeTable " sql += "where CourseId = %d and StuId=%d;"%(cou_id,stu_id) cu.execute(sql) rows = cu.fetchone() if row: print "該課程已選,不能重複選課!" break else: sql3 = "insert into XuankeTable (StuId,CourseId) values (%d,%d)"%(stu_id,cou_id) cu.execute(sql3) cx.commit() print "恭喜你,選課成功!" else: print "sorry,該課程不存在!" else: print "sorry,沒有該學生號" cu.close() def stu_id_search():#按照學生學號查詢學生資訊 cu = cx.cursor() search_stu_id = input("請輸入要查詢的學號:") sql4 = "SELECT ID,StuId,NAME, CLASS from StudentTable " sql4 += "where StuId= %d;" % (search_stu_id) cu.execute(sql4) row = cu.fetchone() if row: print print "您要查詢的學生資訊為:" print "ID = ", row[0] print "StuId = ", row[1] print "NAME = ", row[2] print "CLASS = ",row[3], "\n" else: print "sorry,沒有該學生資訊!" cu.close() def stu_id_cou():#按照學生學號查詢該學生所選課程 cu = cx.cursor() stu_id = input("請輸入要查詢學生號:") sql5 = "select StuId from StudentTable where StuId = %d;"%(stu_id) cu.execute(sql5) row = cu.fetchone() if row : sql6 = "select A.*,B.*,C.* from XuankeTable A, CourseTable B, StudentTable C \ where A.StuId = %d and A.CourseId=B.CourseId and A.StuId=C.StuId"%(stu_id)#連表查詢 cu.execute(sql6) rows = cu.fetchall() for row in rows: print "該學生所選課程為:" print "StuId=",row[1] print "CourseId=",row[2] print "Name = ", row[7] print "Teacher = ", row[8] print "Classroom = ",row[9] print "StartTime = " ,row[10] print "EndTime = ",row[11],"\n" print else: print "sorry,沒有該學生選課資訊!" cu.close() def cou_id_search(): #按照課程號查詢課程資訊 cu = cx.cursor() cou_id = input("請輸入要查詢的課程號:") sql7 = "select CourseId ,Name,Teacher,Classroom,StartTime,EndTime from CourseTable " sql7 += "where CourseId = %d;"%(cou_id) cu.execute(sql7) row = cu.fetchone() if row: print "您要查詢的課程資訊為:" print "CourseId = ",row[0] print "Name = ", row[1] print "Teacher = ", row[2] print "Classroom = ",row[3] print "StartTime = " ,row[4] print "EndTime = ",row[5],"\n" else: print "sorry,沒有該課程資訊!" cu.close() def cou_id_stu():#按照課程號查詢選擇該課程的學生列表 cu = cx.cursor() cou_id = input('請輸入課程號:') sql8 = "select CourseId from XuankeTable where CourseId =%d;"%(cou_id) cu.execute(sql8) row = cu.fetchone() if row: sql9 = "select A.*,B.*,C.* from XuankeTable A, CourseTable B, StudentTable C \ where A.CourseId = %d and A.CourseId=B.CourseId and A.StuId=C.StuId"%(cou_id) cu.execute(sql9) rows = cu.fetchall() for row in rows: print print "選擇該課程的學生為:" print "StuId = ", row[1] print "CourseId = ", row[2] print "NAME = ", row[14] print "CLASS = ",row[15],"\n" else: print "sorry,沒有該課程資訊!" cu.close() def menu(): print '1.進入學生資訊系統(學生資訊錄入)' print '2.進入學生選課系統(學生選課操作)' print '3.進入學生選課資訊系統(學生資訊查詢和選課情況查詢)' print '4.退出程式' def student(): print '1.錄入學生資訊' print '2.返回主選單' def Course(): print '1.開始選課' print '2.返回主選單' def information(): print '1.按學號查詢學生資訊' print '2.按學號檢視學生選課課程列表' print '3.按課程號檢視課程資訊' print '4.按課程號檢視選課學生列表' print '5.返回主選單' while True: menu() print x = raw_input('請輸入您的選擇選單號:') if x == '1': #進入學生資訊系統 student() stu = raw_input('您已進入學生錄入系統,請再次輸入選擇選單:') print if stu == '1': insert_stu() continue if stu == '2': continue else: print "輸入的選項不存在,請重新輸入!" continue if x == '2': #進入選課資訊系統 Course() cou = raw_input('您已進入學生選課系統,請再次輸入選擇選單:') print if cou == '1': xuanke() continue if cou == '2': continue else: print "輸入的選項不存在,請重新輸入!" continue if x == '3': #進入學生選課資訊表 information() inf = raw_input('您已進入學生選課資訊系統,請再次輸入選擇選單:') print if inf == '1': stu_id_search() continue if inf == '2': stu_id_cou() continue if inf == '3': cou_id_search() continue if inf == '4': cou_id_stu() continue if inf == '5': continue else: print "輸入的選項不存在,請重新輸入!" continue if x == '4': print "謝謝使用!" exit() else: print "輸入的選項不存在,請重新輸入!" continue