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 相关文章推荐
linux系统使用python获取内存使用信息脚本分享
Jan 15 Python
使用Python搭建虚拟环境的配置方法
Feb 28 Python
python pandas 如何替换某列的一个值
Jun 09 Python
python 实时得到cpu和内存的使用情况方法
Jun 11 Python
Python魔法方法详解
Feb 13 Python
Python 使用 docopt 解析json参数文件过程讲解
Aug 13 Python
如何定义TensorFlow输入节点
Jan 23 Python
python可视化text()函数使用详解
Feb 11 Python
一劳永逸彻底解决pip install慢的办法
May 24 Python
python scipy 稀疏矩阵的使用说明
May 26 Python
pandas提升计算效率的一些方法汇总
May 30 Python
Python函数式编程中itertools模块详解
Sep 15 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/10/28 PHP
PHP版国家代码、缩写查询函数代码
2011/08/14 PHP
php中用socket模拟http中post或者get提交数据的示例代码
2013/08/08 PHP
PHP类相关知识点实例总结
2016/09/28 PHP
PHP制作登录异常ip检测功能的实例代码
2016/11/16 PHP
PHP获取MySQL执行sql语句的查询时间方法
2018/08/21 PHP
Thinkphp5框架ajax接口实现方法分析
2019/08/28 PHP
php文件后缀不强制为.php的实操方法
2019/09/18 PHP
阿里对象存储OSS在laravel框架中的使用方法
2019/10/13 PHP
深入理解JavaScript系列(41):设计模式之模板方法详解
2015/03/04 Javascript
非常棒的jQuery图片轮播效果
2016/04/17 Javascript
Vue-router 中hash模式和history模式的区别
2018/07/24 Javascript
微信小程序之自定义组件的实现代码(附源码)
2018/08/02 Javascript
vue props传值失败 输出undefined的解决方法
2018/09/11 Javascript
微信小程序自定义单项选择器样式
2019/07/25 Javascript
[03:40]DOTA2亚洲邀请赛小组赛第二日 赛事回顾
2015/01/31 DOTA
[03:14]辉夜杯主赛事 12月25日每日之星
2015/12/26 DOTA
python访问sqlserver示例
2014/02/10 Python
简单了解Python下用于监视文件系统的pyinotify包
2015/11/13 Python
搭建python django虚拟环境完整步骤详解
2019/07/08 Python
python 将html转换为pdf的几种方法
2020/12/29 Python
Maje德国官网:法国女性成衣品牌
2017/02/10 全球购物
递归计算如下递归函数的值(斐波拉契)
2012/02/04 面试题
C#中有没有静态构造函数,如果有是做什么用的?
2016/06/04 面试题
求职自荐书范文
2013/12/04 职场文书
春节联欢会策划方案
2014/05/16 职场文书
幼儿园清明节活动总结
2014/07/04 职场文书
食品质量与安全专业毕业生求职信
2014/08/11 职场文书
七夕相亲活动策划方案
2014/08/31 职场文书
教师年度个人总结
2015/02/11 职场文书
2015年班组长工作总结
2015/04/10 职场文书
2015年扶贫帮困工作总结
2015/05/20 职场文书
2016党员党章学习心得体会
2016/01/14 职场文书
咖啡厅里的创业计划书
2019/08/21 职场文书
python解决12306登录验证码的实现
2021/04/18 Python
Java9新特性之Module模块化编程示例演绎
2022/03/16 Java/Android