国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python學生信息管理系統修改版

2020-02-22 23:27:25
字體:
來源:轉載
供稿:網友

在學習之前先要了解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            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽阳县| 威远县| 巴中市| 塔城市| 青冈县| 合作市| 济南市| 灵台县| 梁山县| 高唐县| 武清区| 无为县| 栾川县| 高尔夫| 莫力| 卢湾区| 礼泉县| 徐闻县| 南乐县| 厦门市| 台中市| 大埔县| 方城县| 会理县| 凌海市| 浦北县| 哈尔滨市| 子洲县| 双桥区| 东莞市| 怀远县| 新乡市| 长岭县| 扎鲁特旗| 中卫市| 琼海市| 资阳市| 自贡市| 伊川县| 将乐县| 榆树市|