python实现学生信息管理系统


Posted in Python onApril 05, 2020

继上篇博客Python实现简易通讯录后,我就想写一个复杂点的学生信息管理系统,这次实现的功能有

1.学生信息的录入管理;
2.学生选课操作;
3.学生选课情况查询;

这次仍然用到sqlite3模块。虽然看着挺简单,但是也踩了不少坑,毕竟刚开始实战,有些细节的还需要多多磨炼啊!

好了,废话不多说,直接上代码,欢迎感兴趣的朋友私信讨论~~~

#-*- coding:utf-8 -*-
import sqlite3
#打开本地数据库用于存储用户信息
conn = sqlite3.connect('student.db')

#在该数据库下创建学生信息表
conn.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";

#在该数据库下创建课程信息表
conn.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";

#在该数据库下创建选课情况信息表
conn.execute ('''CREATE TABLE XuankeTable(
 ID INTEGER PRIMARY KEY AUTOINCREMENT,
 StuId INT NOT NULL,
 CourseId INT NOT NULL,
 StudentNAME TEXT NULL,
 StudenCourse TEXT NULL);''')
print "Table created successfully";

#以上三个表创建完后,再次运行程序时,需要把三个建表代码注释掉,否则会提示:该表已存在。即建表只需建一次。

def insert_stu(): #录入学生信息
 conn = sqlite3.connect('student.db')
 stu_id = input("请输入学生学号:")
 cursor = conn.execute("SELECT StuId from StudentTable where StuId = '%s';"%stu_id)
 conn.commit()
 for row in cursor:
 if stu_id == row[0]:
 print "sorry,该学号已存在,请重新输入"
 break
 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)
 conn.execute(sql1)
 conn.commit()
 print "恭喜你,学生录入成功!"

def xuanke(): #学生选课
 stu_id = input('请输入要选课的学生学号:')
 sql2 = "select StuId from StudentTable where StuId = %d;"%(stu_id)
 cursor1 = conn.execute(sql2)
 for row in cursor1:
 if stu_id == row[0]:
 sql3 = "select CourseId,Name,Teacher,Classroom, StartTime,EndTime from CourseTable"
 cursor2 = conn.execute(sql3)
 for row in cursor2:
 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("请输入要选的课程号:")
 sql = "select StuId from XuankeTable where CourseId = %d;"%(cou_id)
 cursor3= conn.execute(sql)
 for row in cursor3:
 if stu_id == row[0]:
 print "该课程已选,请重新输入要选课程!"
 else:
 sql3 = "insert into XuankeTable (StuId,CourseId) values (%d,%d)"%(stu_id,cou_id)
 cursor4= conn.execute(sql3)
 conn.commit()
 print "恭喜你,选课成功!"
 break
 break
 break
 else:
 print "sorry,没有该学生号"

def stu_id_search():#按照学生学号查询学生信息
 conn = sqlite3.connect('student.db')
 search_stu_id = input("请输入要查询的学号:")
 sql4 = "SELECT StuId from StudentTable where StuId= %d;" % (search_stu_id)
 cursor1 = conn.execute(sql4)
 conn.commit()
 for row in cursor1:
 if search_stu_id == row[0]:
 sql10 = "select ID,StuId,NAME, CLASS from StudentTable where StuId = %d;"%(search_stu_id)
 cursor2 = conn.execute(sql10)
 conn.commit()
 for row in cursor2:
 print
 print "您要查询的学生信息为:"
 print "ID = ", row[0]
 print "StuId = ", row[1]
 print "NAME = ", row[2]
 print "CLASS = ",row[3], "\n"
 break
 else:
 print "sorry,没有该学生信息!"

def stu_id_cou(): #按照学生学号查询该学生所选课程
 stu_id = input("请输入要查询学生号:")
 sql5 = "select StuId from StudentTable where StuId = %d;"%(stu_id)
 cursor = conn.execute(sql5)
 for row in cursor:
 if stu_id == row[0]:
 sql6 = "select CourseId from XuankeTable where StuId = %d;"%(stu_id)
 cursor = conn.execute(sql6)
 conn.commit()
 for row in cursor:
 print
 print "该学生所选课程号为:"
 print row
 print
 break
 else:
 print "sorry,没有该学生选课信息!"

def cou_id_search(): #按照课程号查询课程信息
 cou_id = input("请输入要查询的课程号:")
 sql7 = "select CourseId ,Name,Teacher,Classroom,StartTime,EndTime from CourseTable "
 sql7 += "where CourseId = %d;"%(cou_id)
 cursor1 = conn.execute(sql7)
 conn.commit()
 for row in cursor1:
 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"
 break
 else:
 print "sorry,没有该课程信息!"

def cou_id_stu():#按照课程号查询选择该课程的学生列表
 cou_id = input('请输入课程号:')
 sql8 = "select CourseId from XuankeTable where CourseId =%d;"%(cou_id)
 cursor1 = conn.execute(sql8)
 for row in cursor1:
 if cou_id == row[0]:
 sql9 = "select StuId from XuankeTable where CourseId =%d;"%(cou_id)
 cursor2 = conn.execute(sql9)
 conn.commit()
 for row in cursor2:
 print
 print "选择该课程的学生为:"
 print row,"\n"
 break
 break
 else:
 print "sorry,没有该课程信息!"

def menu():
 print '1.进入学生信息系统(学生信息录入)'
 print '2.进入学生选课系统(学生选课操作)'
 print '3.进入学生选课信息系统(学生信息查询和选课情况查询)'
 print '4.退出程序'

def student():
 print '1.录入学生信息'
 print '2.返回主菜单'
 print '3.退出程序'

def Course():
 print '1.开始选课'
 print '2.返回主菜单'
 print '3.退出程序'

def information():
 print '1.按学号查询学生信息'
 print '2.按学号查看学生选课课程列表'
 print '3.按课程号查看课程信息'
 print '4.按课程号查看选课学生列表'
 print '5.返回主菜单'
 print '6.退出程序'

while True:
 menu()
 print
 x = raw_input('请输入您的选择菜单号:')
 if x == '1':
 #进入学生信息系统
 student()
 stu = raw_input('您已进入学生录入系统,请再次输入选择菜单:')
 if stu == '1':
 insert_stu()
 continue
 if stu == '2':
 menu()
 continue
 if stu == '3':
 print "谢谢使用!"
 exit()
 continue
 else:
 print "输入的选项不存在,请重新输入!"
 continue

 if x == '2':
 #进入选课信息系统
 Course()
 cou = raw_input('您已进入学生选课系统,请再次输入选择菜单:')
 if cou == '1':
 xuanke()
 continue
 if cou == '2':
 menu()
 continue
 if cou == '3':
 print "谢谢使用!"
 exit()
 continue
 else:
 print "输入的选项不存在,请重新输入!"
 continue

 if x == '3':
 #进入学生选课信息表
 information()
 inf = raw_input('您已进入学生选课信息系统,请再次输入选择菜单:')
 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':
 menu()
 continue
 if inf == '6':
 print "谢谢使用!"
 exit()
 continue
 else:
 print "输入的选项不存在,请重新输入!"
 continue

 if x == '4':
 print "谢谢使用!"
 exit()
 else:
 print "输入的选项不存在,请重新输入!"
 continue

更多学习资料请关注专题《管理系统开发》。

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

Python 相关文章推荐
python中使用enumerate函数遍历元素实例
Jun 16 Python
python根据出生日期返回年龄的方法
Mar 26 Python
python使用Tkinter显示网络图片的方法
Apr 24 Python
用Python的Django框架来制作一个RSS阅读器
Jul 22 Python
python正则表达式re之compile函数解析
Oct 25 Python
opencv python 2D直方图的示例代码
Jul 20 Python
Python使用pyserial进行串口通信的实例
Jul 02 Python
简单了解python 邮件模块的使用方法
Jul 24 Python
python 利用zmail库发送邮件
Sep 11 Python
如何用python绘制雷达图
Apr 24 Python
Python实现天气查询软件
Jun 07 Python
Python虚拟环境virtualenv是如何使用的
Jun 20 Python
python针对excel的操作技巧
Mar 13 #Python
python实现聊天小程序
Mar 13 #Python
Python MySQLdb 使用utf-8 编码插入中文数据问题
Mar 13 #Python
python实现简易通讯录修改版
Mar 13 #Python
python学生信息管理系统
Mar 13 #Python
python时间日期函数与利用pandas进行时间序列处理详解
Mar 13 #Python
python用户管理系统
Mar 13 #Python
You might like
php 正则表达式小结
2009/08/31 PHP
如何用php生成扭曲及旋转的验证码图片
2013/06/07 PHP
php堆排序实现原理与应用方法
2015/01/03 PHP
PHP上传文件时自动分配路径的方法
2015/01/09 PHP
php使用GD库创建图片缩略图的方法
2015/06/10 PHP
php+ajax登录跳转登录实现思路
2016/07/31 PHP
另一个javascript小测验(代码集合)
2011/07/27 Javascript
使用Jquery来实现可以输入值的下拉选单 雏型
2011/12/06 Javascript
jquery动态加载select下拉框示例代码
2013/12/10 Javascript
利用javaScript实现点击输入框弹出窗体选择信息
2013/12/11 Javascript
jquery select 设置默认选中的示例代码
2014/02/07 Javascript
form.submit()不能提交表单的错误原因及解决方法
2014/10/13 Javascript
推荐5 个常用的JavaScript调试技巧
2015/01/08 Javascript
原生js实现商品放大镜效果
2017/01/12 Javascript
JavaScript实现水平进度条拖拽效果
2017/01/18 Javascript
js模仿微信朋友圈计算时间显示几天/几小时/几分钟/几秒之前
2017/04/27 Javascript
vue2.0移除或更改的一些东西(移除index key)
2017/08/28 Javascript
Vue.js组件通信的几种姿势
2017/10/23 Javascript
JavaScript实现短暂提示框功能
2018/04/04 Javascript
Layui table field初始化加载时进行隐藏的方法
2019/09/19 Javascript
Vue插件之滑动验证码用法详解
2020/04/05 Javascript
vue中实现图片压缩 file文件的方法
2020/05/28 Javascript
[05:36]DOTA2 2015国际邀请赛中国区预选赛第四日TOP10
2015/05/29 DOTA
[00:32]2018DOTA2亚洲邀请赛Secret出场
2018/04/03 DOTA
python mysqldb连接数据库
2009/03/16 Python
在Python中使用成员运算符的示例
2015/05/13 Python
python字典基本操作实例分析
2015/07/11 Python
Linux上使用Python统计每天的键盘输入次数
2019/04/17 Python
我爱读书演讲稿
2014/05/07 职场文书
机械设计制造及其自动化专业求职信
2014/06/17 职场文书
个人房屋转让协议书范本
2014/10/26 职场文书
优秀工作者事迹材料
2014/12/26 职场文书
毕业生自荐材料范文
2014/12/30 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
关于军训的感想
2015/08/07 职场文书
2016年三严三实党课学习心得体会
2016/01/06 职场文书