Python学生信息管理系统修改版


Posted in Python onMarch 13, 2018

在学习之前先要了解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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现扫描指定目录下的子目录及文件的方法
Jul 16 Python
利用python将xml文件解析成html文件的实现方法
Dec 22 Python
Python图形绘制操作之正弦曲线实现方法分析
Dec 25 Python
Python爬虫实现百度图片自动下载
Feb 04 Python
Python交互环境下实现输入代码
Jun 22 Python
Python 实现中值滤波、均值滤波的方法
Jan 09 Python
对Python中创建进程的两种方式以及进程池详解
Jan 14 Python
Python批量处理csv并保存过程解析
May 16 Python
Python手动或自动协程操作方法解析
Jun 22 Python
基于python实现监听Rabbitmq系统日志代码示例
Nov 28 Python
selenium与xpath之获取指定位置的元素的实现
Jan 26 Python
Python将CSV文件转化为HTML文件的操作方法
Jun 30 Python
python之pandas用法大全
Mar 13 #Python
python使用sqlite3时游标使用方法
Mar 13 #Python
Python打印输出数组中全部元素
Mar 13 #Python
python实现学生信息管理系统
Apr 05 #Python
python针对excel的操作技巧
Mar 13 #Python
python实现聊天小程序
Mar 13 #Python
Python MySQLdb 使用utf-8 编码插入中文数据问题
Mar 13 #Python
You might like
php file_get_contents抓取Gzip网页乱码的三种解决方法
2013/11/12 PHP
PHP+javascript制作带提示的验证码源码分享
2014/05/28 PHP
PHP 文件锁与进程锁的使用示例
2017/08/07 PHP
Js+Flash实现访问剪切板操作
2012/11/20 Javascript
学习js在线html(富文本,所见即所得)编辑器
2012/12/18 Javascript
JS:window.onload的使用介绍
2013/11/13 Javascript
Javascript中的异步编程规范Promises/A详细介绍
2014/06/06 Javascript
node.js中的console.error方法使用说明
2014/12/10 Javascript
JavaScript中的console.group()函数详细介绍
2014/12/29 Javascript
jQuery插件jFade实现鼠标经过的图片高亮其它变暗
2015/03/14 Javascript
JavaScript判断表单提交时哪个radio按钮被选中的方法
2015/03/21 Javascript
js实现将选中值累加到文本框的方法
2015/08/12 Javascript
基于bootstrap-datetimepicker.js不支持IE8的快速解决方法
2016/11/07 Javascript
ajax接收后台数据在html页面显示
2017/02/19 Javascript
微信小程序实现传参数的几种方法示例
2018/01/10 Javascript
vue-cli下的vuex的简单Demo图解(实现加1减1操作)
2018/02/26 Javascript
vuejs实现标签选项卡动态更改css样式的方法
2018/05/31 Javascript
JQuery常用简单动画操作方法回顾与总结
2019/12/07 jQuery
Node.js API详解之 vm模块用法实例分析
2020/05/27 Javascript
解决vue安装less报错Failed to compile with 1 errors的问题
2020/10/22 Javascript
js实现淘宝浏览商品放大镜功能
2020/10/28 Javascript
[01:02:25]2014 DOTA2华西杯精英邀请赛5 24 NewBee VS VG
2014/05/25 DOTA
布同 统计英文单词的个数的python代码
2011/03/13 Python
Python魔法方法功能与用法简介
2019/04/04 Python
Django 配置多站点多域名的实现步骤
2019/05/17 Python
详解python中index()、find()方法
2019/08/29 Python
Python图像处理库PIL的ImageFont模块使用介绍
2020/02/26 Python
python能开发游戏吗
2020/06/11 Python
css3动画鼠标放上图片逐渐变大鼠标离开图片逐渐缩小效果
2021/01/27 HTML / CSS
瑞贝卡·明可弗包包官网:Rebecca Minkoff
2016/07/21 全球购物
Tostadora意大利:定制T恤
2019/04/08 全球购物
家得宝墨西哥官网:The Home Depot墨西哥
2019/11/18 全球购物
幼儿园保育员辞职信
2014/01/12 职场文书
小学优秀辅导员事迹材料
2014/05/11 职场文书
2015年防灾减灾工作总结
2015/07/24 职场文书
Python常用配置文件ini、json、yaml读写总结
2021/07/09 Python