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中解析XML数据的方法
Oct 15 Python
python调用fortran模块
Apr 08 Python
在 Python 应用中使用 MongoDB的方法
Jan 05 Python
Python2/3中urllib库的一些常见用法
Dec 19 Python
tensorflow构建BP神经网络的方法
Mar 12 Python
浅谈python正则的常用方法 覆盖范围70%以上
Mar 14 Python
python 动态调用函数实例解析
Oct 21 Python
Pandas数据离散化原理及实例解析
Nov 16 Python
Python 字典中的所有方法及用法
Jun 10 Python
Pycharm及python安装详细教程(图解)
Jul 31 Python
Pycharm编辑器功能之代码折叠效果的实现代码
Oct 15 Python
pycharm中leetcode插件使用图文详解
Dec 07 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与ASP
2006/10/09 PHP
destoon二次开发常用数据库操作
2014/06/21 PHP
PHP制作图形验证码代码分享
2014/10/23 PHP
Laravel模型间关系设置分表的方法示例
2018/04/21 PHP
showModelessDialog()使用详解
2006/09/07 Javascript
window.addeventjs事件驱动函数集合addEvent等
2008/02/19 Javascript
jquery实现textarea输入字符控制(仿微博输入控制字符)
2013/04/26 Javascript
js实现的牛顿摆效果
2015/03/31 Javascript
javascript设计模式之模块模式学习笔记
2017/02/15 Javascript
使用vue.js写一个tab选项卡效果
2017/03/25 Javascript
Vue2.0使用过程常见的一些问题总结学习
2017/04/10 Javascript
javascript数据结构之串的概念与用法分析
2017/04/12 Javascript
jQuery实现拖动效果的实例代码
2017/06/25 jQuery
关于Promise 异步编程的实例讲解
2017/09/01 Javascript
Three.js利用dat.GUI如何简化试验流程详解
2017/09/26 Javascript
解决VUE中document.body.scrollTop为0的问题
2018/09/15 Javascript
小程序日历控件使用方法详解
2018/12/29 Javascript
Vue自定义全局弹窗组件操作
2020/08/11 Javascript
[04:42]5分钟带你了解什么是DOTA2(第一期)
2017/02/07 DOTA
[01:00:30]完美世界DOTA2联赛循环赛 Inki vs Matador BO2第二场 10.31
2020/11/02 DOTA
解决Python找不到ssl模块问题 No module named _ssl的方法
2019/04/29 Python
python实现爬虫抓取小说功能示例【抓取金庸小说】
2019/08/09 Python
Django之使用内置函数和celery发邮件的方法示例
2019/09/16 Python
pytorch-神经网络拟合曲线实例
2020/01/15 Python
如何在python开发工具PyCharm中搭建QtPy环境(教程详解)
2020/02/04 Python
浅谈python3打包与拆包在函数的应用详解
2020/05/02 Python
Net-A-Porter美国官网:全球时尚奢侈品名站
2017/02/11 全球购物
马来西亚最大的电器网站:Senheng
2017/10/13 全球购物
波兰家居饰品和厨房配件网上商店:Maleomi
2020/12/15 全球购物
有关打架的检讨书
2014/01/25 职场文书
语文教研活动总结
2014/07/02 职场文书
2014年民主评议党员工作总结
2014/12/02 职场文书
优秀教师申报材料
2014/12/16 职场文书
会计求职简历自我评价
2015/03/10 职场文书
小学教师见习总结
2015/06/23 职场文书
PostgreSQL并行计算算法及参数强制并行度设置方法
2022/04/06 PostgreSQL