目录
第一章 系统总体设计
1.1 项目背景及意义
1.2 系统的设计内容
1.3 开发环境
第二章 需求分析
2.1 系统业务需求
2.2 系统数据需求
第三章 概要设计
3.1 功能结构设计
3.2 数据库概念结构设计
3.3 数据库逻辑结构设计
3.4 数据库物理设计
第四章 系统实现
4.1 界面设计
4.2 主要功能及程序代码
第五章 测试与总结 49
5.1 测试 49
5.2 总结 58
参考文献 60
每个学校都需要在期末进行考试成绩的统计分析工作,而这些工作都必须在考试结束后一个星期的时间内完成。大量的成绩数据的统计工作如果只靠人工完成,费时费力,还容易出错。随机计算机技术的快速发展,计算机在日常管理应用中迅速普及,利用计算机进行学生成绩管理势在必行。因此需要开发出一个满足学校进行成绩的录入、查询、修改等需求的功能完善,安全可靠,迅速便捷的成绩管理系统。
1.2.1 系统设计目的
开发出一个操作简便、界面友好、灵活实用、安全可靠的学生成绩管理系统。该系统的开发以任课教师和学生服务为对象,能够提高学校对学生成绩的统计分析效率,减轻教务管理人员对学生成绩管理和统计的负担,能提高学校对学生成绩的规范化管理。
该成绩系统能够及时对学生成绩进行收集整理,使学校相关部门及时获取可靠的学生成绩信息,便于管理。方便各任课教师记录,统计所带班级学生成绩,提高工作效率,减轻负担,提高教学质量。实现快速方便地处理大量成绩数据信息,完成成绩的录入、修改、删除、查询等处理要求。方便学生查询自己各科目成绩。
1.2.2 系统概述
本系统能够实现学生线上选课、查看成绩等操作。采用python+flask+mysql的方式,未做服务器部署,可以在单机上测试运行
前端则借鉴了网上找到的前端页面模板,在此基础上修改,主要技术是html+css+js,前后端分离开发,使用postman等工具进行接口测试
1.2.3 系统功能要求
第二章 需求分析
2.1 系统业务需求
本系统将开发一个学生成绩管理的系统。通过调研学生学习情况,老师教学
情况,对学生成绩进行分析,确定学生成绩管理系统主要有三类用户角色:学生、
教师、管理员。他们各自的主要信息处理需求如下:
管理员用户
程信息包括课程编号、课程名、学分、开课学年、开课学期、授课教师编号及名称。
学生用户:
教师用户:
2.2 系统数据需求
系统涉及的主要数据源有学生数据、教师数据、管理员数据,产生课程信息表、专业信息表、专业选课表等各表单。结合业务处理需求,系统数据流图如图所示。
图2-1 实体说明
图2-2 数据流说明
第三章 概要设计
3.1 功能结构设计
根据需求分析所获得的业务需求和数据需求,可以定义系统的功能结构图如图3-1所示。
图3-1 学生成绩管理系统功能结构
各模块详细功能如下:
3.1.1学生模块
3.1.2 教师模块
(1)教师通过教师编号登录,初始密码为教师编号;
(2)课程管理:教师对教授的课程进行成绩录入修改,统计不同分数段的学生人数;
(3)修改个人账户密码,前密码、两次新密码输入验证后进行密码修改;
3.1.3 管理员模块
3.2.1 概念结构
在需求分析阶段所得到的应用需求应该首先抽象为信息世界的结构,才能更好地、 更准确地用某一 DBMS 实现这些需求。
概念结构主要特点:
对数据的处理要求,是对现实世界的一个只是模型;
与是数据库设计成功的关键。
概念结构时各种数据模型的共同基础,它比数据模型更独立于机器、更抽象,从而更加稳定。
3.2.2学生成绩管理系统概念结构设计
现在对所设计系统的需求作进一步的分析,产生概念结构设计的 E-R 模型。由于这个系统并不复杂,因此可采用自顶向下的设计方法。自顶向下设计的关键是确定系统的核心活动。所谓核心活动就是系统中的其他活动都要围绕这个活动展开,或与此活动密切相关。确定了核心活动之后,系统就有了可扩展的余地。
3.2.3 E-R 模型
系统主要涉及学生、教师、管理员三个实体,学生与教师通过授课建立联系,一个学生学习多门课程,一个教师可以教多个学生;管理员通过管理分别与学生、教师建立联系;另外还有课程、专业、专业选课、成绩等实体;分析定义各实体的属性,建立实体联系模型的E-R图如下所示。
图3-4 学生成绩管理系统E-R图
3.3 数据库逻辑结构设计
关系模式:
将数据库的概念模型转化为如下几个基本关系模式,属性标 的为主键,标有 的是外键;
学生(学号、姓名、性别、出生年份、籍贯、入学年份、专业编号、密码)
教师(教师编号、姓名、性别、出生年份、密码)
课程(课程编号、课程名、开课学年、开课学期、学分、教师编号)
专业(专业编号、专业名)
学生选课(学号、课程编号、成绩)
专业选课(专业编号、课程编号)
管理员(用户名、密码)
3.4.1 数据库表结构
数据库包括5个关系表,其中表3-5~3-10所示为表结构。
字段 | 描述 | 取值说明 | 备注 |
student_id | 学生编号,主键 | char(10),数字 | 主键 |
student_name | 学生姓名 | varchar(20),中文 | not null |
sex | 性别 | char(2),男|女 | |
birth_year | 出生年份 | char(4),数字 | |
province | 籍贯省 | varchar(20),中文 | |
enter_year | 入学年份 | char(4),数字 | not null |
major_id | 专业编号,外键 | char(3),数字 | 外键,not null |
password | 加密后的密码 | varchar(128) | not null |
表3-5 student学生表结构
字段 | 描述 | 取值说明 | 备注 |
teacher_id | 教师编号 | char(5),数字 | 主键 |
teacher_name | 教师姓名 | varchar(20),中文 | not null |
sex | 性别 | char(2),男|女 | |
birth_year | 出生年份 | char(4),数字 | |
password | 加密后的密码 | varchar(128) | not null |
表3-6教师表结构
(3)course 课程表
字段 | 描述 | 取值说明 | 备注 |
course_id | 课程编号 | char(5),数字 | 主键 |
course_name | 课程名称 | varchar(20),中文 | not null |
year | 开设年份 | char(4),数字 | not null |
semester | 开设学期 | char(2),秋|春 | not null |
teacher_id | 教师编号 | char(5),数字 | 外键,not null |
credit | 学分 | tiny int,unsigned,0~10 | not null |
表3-7课程表结构
(4)student_course学生选课表
字段 | 描述 | 取值说明 | 备注 |
student_id | 学生编号 | char(10),数字 | 联合主键,外键 |
course_id | 课程编号 | char(5),数字 | 联合主键,外键 |
grade | 成绩 | tiny int,unsigned,0~100 |
表3-8 选课表结构
字段 | 描述 | 取值说明 | 备注 |
major_id | 专业编号 | char(3),数字 | 联合主键,外键 |
course_id | 课程编号 | char(5),数字 | 联合主键,外键 |
表3-9 专业选课表结构
(6)admin 管理员表
字段 | 描述 | 取值说明 | 备注 |
admin_name | 用户名 | char(15) | 主键 |
password | 密码 | char(128) | not null |
表3-10 管理员表结构
3.4.2 数据库建表代码
1.创建数据库
create database grade_manage_system;
2.进入数据库
use grade_manage_system
3.创建学生表
create table student ( student_id char(10) primary key, student_name varchar(20) not null, sex char(2), birth_year char(4), province varchar(20), enter_year char(4) not null, major_id char(3) not null, password varchar(128) not null );
4.创建教师表
create table teacher ( teacher_id char(5) primary key, teacher_name varchar(20) not null, sex char(2), birth_year char(4), password varchar(128) not null );
5.创建课程表
create table course ( course_id char(5) primary key, course_name varchar(20) not null, year char(4) not null, semester char(2) not null, teacher_id char(5) not null, credit tinyint unsigned not null );
6.创建专业表
create table major ( major_id char(3) primary key, major_name varchar(20) not null unique );
7.创建专业选课表
create table major_course ( major_id char(3), course_id char(5) );
8.创建学生选课成绩表
create table student_course ( student_id char(10), course_id char(5), grade tinyint unsigned );
create table admin ( admin_name char(15) primary key, password varchar(128) not null );
学生成绩管理系统页面由 Python编程 flask库制作。页面主要分为四大模块,分别是:登陆页面模块、学生用户模块、教师用户模块和管理员用户模块。登陆页面由html+css+js技术,通过使用postman等工具进行接口。而其他三大模块下又设有若干小模块,各包含若干页面。详细界面设计如下文。
4.1.1 登录页面设计
学生成绩管理系统是通过选择登录身份,输入账号密码进行登录。管理员以admin作为用户名,密码为固定值,不可修改,教师以教师编号为用户名,密码默认为教师编号,可修改,学生以学生编号为用户名,密码默认为学生编号,可修改。登录界面如下:
4.1.2 学生模块
4.1.3 教师模块
4.1.4 管理员模块
管理员通过用户名为admin,密码为asdfg13579进行登录,
管理员登录成功后有查看所有学生信息、添加学生、查看所有教师信息、添加教师、查看所有课程信息、添加课程、查看所有专业信息、添加专业、查看专业选课信息、添加专业选课几个功能界面
4.2.1 数据库搭建
学生成绩管理系统的实现,共需要在 MySQL 上创建一个数据库,七个表,分别
是学生表(student)、教师表(teacher)、课程表(course)、专业表
(major)、管理员表(admin)、学生选课表(student_course)、专业选课表(major_course)。
(1)创建并使用数据库
create database grade_manage_system;// use grade_manage_system
(2)创建专业表
create table major(major_id char(3) primary key, major_name varchar(20) not null unique);//
(3)创建学生表
create table student( student_id char(10) primary key, student_name varchar(20) not null, sex char(2), birth_year char(4), province varchar(20), enter_year char(4) not null, major_id char(3) not null, password varchar(128) not null);// alter table student add foreign key (major_id) references major (major_id) on delete cascade;//
(4)创建教师表
create table teacher( teacher_id char(5) primary key, teacher_name varchar(20) not null, sex char(2), birth_year char(4), password varchar(128) not null);//
(5)创建课程表
create table course( course_id char(5) primary key, course_name varchar(20) not null, year char(4) not null, semester char(2) not null, teacher_id char(5) not null, credit tinyint unsigned not null);// alter table course add foreign key (teacher_id) references teacher (teacher_id) on delete cascade;//
(6)创建专业选课表
create table major_course( major_id char(3), course_id char(5));// alter table major_course add primary key (major_id, course_id);// alter table major_course add foreign key (major_id) references major (major_id) on delete cascade;// alter table major_course add foreign key (course_id) references course (course_id) on delete cascade;//
(7)创建学生选课表
create table student_course( student_id char(10), course_id char(5), grade tinyint unsigned);// alter table student_course add primary key (student_id, course_id);//
(8)创建添加专业选课
create trigger trigger_insert_1 after insert on major_course for each row begin insert into student_course ( select student_id, new.course_id, null from student where major_id = new.major_id ); end//
(9)创建删除专业选课
create trigger trigger_delete_1 after delete on major_course for each row begin delete from student_course where student_id in ( select student_id from student where major_id = old.major_id ) and course_id = old.course_id; end//
(10)创建添加学生
create trigger trigger_insert_2 after insert on student for each row begin insert into student_course ( select new.student_id, course_id, null from major_course where major_id = new.major_id ); end//
(11)创建删除学生
create trigger tigger_delete_2 after delete on student for each row begin delete from student_course where course_id in ( select course_id from major_course where major_id = old.major_id ) and student_id = old.student_id; end// delimiter ;
4.2.2 数据库的连接
功能:使得数据库与客户端进行数据传输
代码:
if __name__ == '__main__': app.config['DEBUG'] = True app.config['SECRET_KEY'] = '123456' app.config['DATABASE_USER'] = 'root' app.config['DATABASE_PASSWORD'] = '1234
4.2.3 登录模块
功能:验证账号密码和对应的权限,以及跳转进入对应主页面
代码:
from flask import Flask from flask_login import LoginManager from app.models.session import LoginChecker from werkzeug.utils import redirect from flask_login import login_required login_manager = LoginManager() @login_manager.user_loader def load_user(user_id): if 'student' in user_id: return LoginChecker('load', 'student', user_id[7:], 'default') if 'teacher' in user_id: return LoginChecker('load', 'teacher', user_id[7:], 'default') if 'admin' in user_id: return LoginChecker('load', 'admin', user_id[5:], 'default') return None app = Flask(__name__) login_manager.init_app(app) from app.web.student import web app.register_blueprint(web) @app.route('/') def bare(): return redirect('/static/Login.html') @app.route('/static/Login.html') def html_login(): return app.send_static_file('Login.html') @app.route('/static/Student_Info.html') @login_required def html_student_info(): return app.send_static_file('Student_Info.html') @app.route('/static/Student_Scores.html') @login_required def html_student_scores(): return app.send_static_file('Student_Scores.html') @app.route('/static/Student_ChangePassword.html') @login_required def html_student_change_password(): return app.send_static_file('Student_ChangePassword.html') @app.route('/static/Teacher_Info.html') @login_required def html_teacher_info(): return app.send_static_file('Teacher_Info.html') @app.route('/static/Teacher_Courses.html') @login_required def html_teacher_courses(): return app.send_static_file('Teacher_Courses.html') @app.route('/static/Teacher_CourseInfo.html') @login_required def html_course_info(): return app.send_static_file('Teacher_CourseInfo.html') @app.route('/static/Teacher_ChangePassword.html') @login_required def html_teacher_change_password(): return app.send_static_file('Teacher_ChangePassword.html') @app.route('/static/Teacher_Statistics.html') @login_required def html_teacher_statistics(): return app.send_static_file('Teacher_Statistics.html')
4.2.4 学生菜单模块
功能:在学生页面进行不同功能页面的切换
各个功能代码如下:
from app.models.general import connect_to_sql class StudentReader: def __init__(self, student_id): self.data = read_base_information(student_id) if self.data.get('error'): return self.data = dict(self.data, **read_total_credits(student_id)) if self.data.get('error'): return self.data = dict(self.data, **read_number_of_courses(student_id)) if self.data.get('error'): return self.data = dict(self.data, **read_average_grade(student_id)) class StudentCoursesReader: def __init__(self, student_id): self.data = read_selected_courses(student_id)
def read_base_information(student_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: # 查询学生基本信息 sql = 'select student_id, student_name, sex, birth_year, province, enter_year, major_name, s.major_id ' \ 'from student as s, major as m ' \ 'where s.student_id = %s and s.major_id = m.major_id;' % student_id cursor.execute(sql) result = cursor.fetchone() if result: data['student_id'] = result[0] data['student_name'] = result[1] data['sex'] = result[2] data['birth_year'] = result[3] data['province'] = result[4] data['enter_year'] = result[5] data['major_name'] = result[6] data['major_id'] = result[7] else: data['error'] = 'student id ' + student_id + ' not found in database' except Exception as e: data['error'] = str(e) finally: connection.close() return data
def read_total_credits(student_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select if(sum(credit) is null, 0, sum(credit)) from student as s, course as c, student_course as sc ' \ 'where s.student_id = %s and s.student_id = sc.student_id ' \ 'and c.course_id = sc.course_id and grade >= 60;' % student_id cursor.execute(sql) result = cursor.fetchone() if result: data['credits'] = int(result[0]) else: data['credits'] = 0 except Exception as e: data['error'] = str(e) finally: connection.close() return data def read_number_of_courses(student_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select count(course_id) from student_course ' \ 'where student_id = %s;' % student_id cursor.execute(sql) result = cursor.fetchone() if result: data['number_of_courses'] = int(result[0]) else: data['number_of_courses'] = 0 except Exception as e: data['error'] = str(e) finally: connection.close() return data def read_average_grade(student_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select credit, grade from student_course as sc, course as c ' \ 'where sc.student_id = %s and sc.course_id = c.course_id ' \ 'and grade is not null;' % student_id cursor.execute(sql) result = cursor.fetchall() if result: total_credit = 0 total_grade = 0 for row in result: total_credit += row[0] total_grade += row[0] * row[1] data['average_grade'] = round(float(1.0*total_grade/total_credit), 2) else: data['average_grade'] = 0 except Exception as e: data['error'] = str(e) finally: connection.close() return data def read_selected_courses(student_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select c.course_id, c.course_name, c.year, c.semester, t.teacher_name, c.credit, sc.grade ' \ 'from course as c, student_course as sc, teacher as t ' \ 'where sc.student_id = %s and sc.course_id = c.course_id ' \ 'and t.teacher_id = c.teacher_id;' % student_id cursor.execute(sql) result = cursor.fetchall() data['courses'] = [] for row in result: tmp = {'course_id': row[0], 'course_name': row[1], 'year': row[2], 'semester': row[3], 'teacher_name': row[4], 'credit': row[5], 'grade': row[6]} data['courses'].append(tmp) except Exception as e: data['error'] = str(e) finally: connection.close() return data
4.2.5 教师菜单模块
功能:在教师页面进行不同功能页面的切换
from app.models.general import connect_to_sql class TeacherReader: def __init__(self, teacher_id): self.data = read_base_information(teacher_id) if self.data.get('error'): return self.data = dict(self.data, **read_number_of_courses(teacher_id)) class TeacherCoursesReader: def __init__(self, teacher_id): self.data = read_taught_courses(teacher_id) class TeacherCourseStudentReader: def __init__(self, course_id): self.data = read_selected_students(course_id) class TeacherGradeUpdater: def __init__(self, course_id, student_id, grade): self.data = update_student_grade(course_id, student_id, grade) class CourseStatisticReader: def __init__(self, course_id): self.data = read_course_statistic(course_id)
def read_number_of_courses(teacher_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: # 查询教师所授课程数量 sql = 'select count(course_id) from course ' \ 'where teacher_id = %s;' % teacher_id cursor.execute(sql) result = cursor.fetchone() if result: data['number_of_courses'] = int(result[0]) else: data['number_of_courses'] = 0 except Exception as e: data['error'] = str(e) finally: connection.close() return data
(3)教师查看授课信息
def read_taught_courses(teacher_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: # 查询教师授课列表 sql = 'select course_id, course_name, year, semester, credit ' \ 'from course ' \ 'where teacher_id = %s ' % teacher_id cursor.execute(sql) result = cursor.fetchall() data['courses'] = [] if result: for row in result: tmp = {'course_id': row[0], 'course_name': row[1], 'year': row[2], 'semester': row[3], 'credit': row[4]} data['courses'].append(tmp) except Exception as e: data['error'] = str(e) finally: connection.close() return data
(4)教师查看选课学生
def read_selected_students(course_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: # 查询选课学生列表 sql = 'select s.student_id, s.student_name, s.sex, m.major_name, sc.grade ' \ 'from student_course as sc, student as s, major as m ' \ 'where sc.student_id = s.student_id and s.major_id = m.major_id ' \ 'and course_id = %s ' % course_id cursor.execute(sql) result = cursor.fetchall() data['students'] = [] if result: for row in result: tmp = {'student_id': row[0], 'student_name': row[1], 'sex': row[2], 'major_name': row[3], 'grade': row[4]} data['students'].append(tmp) except Exception as e: data['error'] = str(e) finally: connection.close() return data
(5)教师查看课程统计信息
def read_course_statistic(course_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: # 查询某课程统计信息 sql = 'select ' \ 'count(case when grade between 0 and 59 then grade end) as \'0-59\',' \ 'count(case when grade between 60 and 69 then grade end) as \'60-69\',' \ 'count(case when grade between 70 and 79 then grade end) as \'70-79\',' \ 'count(case when grade between 80 and 89 then grade end) as \'80-89\',' \ 'count(case when grade between 90 and 100 then grade end) as \'90-100\' ' \ 'from student_course ' \ 'where course_id = %s;' % course_id cursor.execute(sql) result = cursor.fetchone() if result: data['0-59'] = int(result[0]) data['60-69'] = int(result[1]) data['70-79'] = int(result[2]) data['80-89'] = int(result[3]) data['90-100'] = int(result[4]) else: data['error'] = 'course_id ' + course_id + ' not found in database' except Exception as e: data['error'] = str(e) finally: connection.close() return data
(6)教师修改学生成绩
def update_student_grade(course_id, student_id, grade): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: # 更新学生某课程成绩 sql = 'update student_course ' \ 'set grade = %s ' \ 'where course_id = \'%s\' and student_id = \'%s\';' % (grade, course_id, student_id) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
4.2.6 管理员模块
功能:在管理员页面进行不同功能页面的切换
from app.models.general import connect_to_sql from app.models.student import StudentReader from werkzeug.security import generate_password_hash class StudentListReader: def __init__(self): data = read_student_id_list() if data.get('error'): return student_id_list = data['student_id'] self.data = {'students': []} for student_id in student_id_list: tmp = StudentReader(student_id) if tmp.data.get('error'): self.data['error'] = tmp.data['error'] return self.data['students'].append(tmp.data) class StudentUpdater: def __init__(self, student_id, student_name, sex, birth_year, province, enter_year, major_id): self.data = update_student(student_id, student_name, sex, birth_year, province, enter_year, major_id) class StudentInserter: def __init__(self, student_name, sex, birth_year, province, enter_year, major_id): self.data = insert_student(student_name, sex, birth_year, province, enter_year, major_id) class StudentDeleter: def __init__(self, student_id): self.data = delete_student(student_id)
def delete_student(student_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'delete from student where student_id = \'%s\'; ' % student_id cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def insert_student(student_name, sex, birth_year, province, enter_year, major_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: student_id = get_new_student_id(enter_year, major_id) password = generate_password_hash(student_id['student_id']) sql = 'insert into student values ' \ '(\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\'); ' \ % (student_id['student_id'], student_name, sex, birth_year, province, enter_year, major_id, password) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def read_student_id_list(): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select student_id from student order by student_id;' cursor.execute(sql) data['student_id'] = cursor.fetchall() except Exception as e: data['error'] = str(e) finally: connection.close() return data
def update_student(student_id, student_name, sex, birth_year, province, enter_year, major_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'update student set student_name = \'%s\', sex = \'%s\', birth_year = \'%s\', ' \ 'province = \'%s\', enter_year = \'%s\', major_id = \'%s\' where student_id = \'%s\'; ' % \ (student_name, sex, birth_year, province, enter_year, major_id, student_id) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
from werkzeug.security import generate_password_hash from app.models.general import connect_to_sql class TeacherListReader: def __init__(self): self.data = read_teacher_list() class TeacherIdReader: def __init__(self): self.data = read_teacher_id() class TeacherUpdater: def __init__(self, teacher_id, teacher_name, sex, birth_year): self.data = update_teacher(teacher_id, teacher_name, sex, birth_year) class TeacherInserter: def __init__(self, teacher_name, sex, birth_year): self.data = insert_teacher(teacher_name, sex, birth_year) class TeacherDeleter: def __init__(self, teacher_id): self.data = delete_teacher(teacher_id)
def read_teacher_list(): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select t.teacher_id, teacher_name, sex, birth_year, count(course_id) as number_of_courses ' \ 'from teacher as t left outer join course as c ' \ 'on c.teacher_id = t.teacher_id ' \ 'group by t.teacher_id ' \ 'order by t.teacher_id;' cursor.execute(sql) result = cursor.fetchall() data['teachers'] = [] for row in result: tmp = {'teacher_id': row[0], 'teacher_name': row[1], 'sex': row[2], 'birth_year': row[3], 'number_of_courses': row[4]} data['teachers'].append(tmp) except Exception as e: data['error'] = str(e) finally: connection.close() return data
def delete_teacher(teacher_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'delete from teacher where teacher_id = \'%s\'; ' % teacher_id cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def insert_teacher(teacher_name, sex, birth_year): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: teacher_id = get_new_teacher_id() password = generate_password_hash(teacher_id['teacher_id']) if teacher_id.get('error'): data['error'] = teacher_id['error'] return data sql = 'insert into teacher values(\'%s\', \'%s\', \'%s\', \'%s\', \'%s\');' \ % (teacher_id['teacher_id'], teacher_name, sex, birth_year, password) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def update_teacher(teacher_id, teacher_name, sex, birth_year): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'update teacher set teacher_name = \'%s\', sex = \'%s\', birth_year = \'%s\' ' \ 'where teacher_id = \'%s\'; ' % \ (teacher_name, sex, birth_year, teacher_id) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
from app.models.general import connect_to_sql class CourseReader: def __init__(self): self.data = read_course() class SingleCourseReader: def __init__(self, course_id): self.data = read_single_course(course_id) class CourseDeleter: def __init__(self, course_id): self.data = delete_course(course_id) class CourseInserter: def __init__(self, course_name, year, semester, teacher_id, credit): self.data = insert_course(course_name, year, semester, teacher_id, credit) class CourseUpdater: def __init__(self, course_id, course_name, year, semester, teacher_id, credit): self.data = update_course(course_id, course_name, year, semester, teacher_id, credit) class CourseTeacherReader: def __init__(self, course_id): self.data = get_teacher_of_course(course_id)
def read_course(): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select tmp.course_id, tmp.course_name, tmp.year, tmp.semester, tmp.teacher_id, ' \ 'tmp.credit, tmp.number_of_students, tmp.average_grade, t.teacher_name from ( ' \ 'select c.course_id, course_name, year, semester, teacher_id, credit, count(student_id), avg(grade) ' \ 'from course as c left outer join student_course as sc ' \ 'on c.course_id = sc.course_id ' \ 'group by c.course_id ' \ ') as tmp(course_id, course_name, year, semester, teacher_id, ' \ 'credit, number_of_students, average_grade), teacher as t ' \ 'where tmp.teacher_id = t.teacher_id ' \ 'order by tmp.course_id; ' cursor.execute(sql) result = cursor.fetchall() data['courses'] = [] for row in result: average_grade = row[7] if average_grade is None: average_grade = 0 tmp = {'course_id': row[0], 'course_name': row[1], 'year': row[2], 'semester': row[3], 'teacher_id': row[4], 'credit': row[5], 'number_of_students': row[6], 'average_grade': round(float(average_grade), 2), 'teacher_name': row[8]} data['courses'].append(tmp) except Exception as e: data['error'] = str(e) finally: connection.close() return data
def insert_course(course_name, year, semester, teacher_id, credit): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: course_id = get_new_course_id() if course_id.get('error'): data['error'] = course_id['error'] return data sql = 'insert into course values(\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', %d);' \ % (course_id['course_id'], course_name, year, semester, teacher_id, credit) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def delete_course(course_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'delete from course where course_id = \'%s\'; ' % course_id cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) finally: connection.close() return data
def update_course(course_id, course_name, year, semester, teacher_id, credit): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'update course set course_name = \'%s\', year = \'%s\', semester = \'%s\',' \ 'teacher_id = \'%s\', credit = %d where course_id = \'%s\';' \ % (course_name, year, semester, teacher_id, credit, course_id) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
from app.models.general import connect_to_sql class MajorIdReader: def __init__(self): self.data = read_major_id() class MajorReader: def __init__(self): self.data = read_major() class SingleMajorReader: def __init__(self, major_id): self.data = read_single_major(major_id) class MajorUpdater: def __init__(self, major_id, major_name): self.data = update_major(major_id, major_name) class MajorInserter: def __init__(self, major_name): self.data = insert_major(major_name) class MajorDeleter: def __init__(self, major_id): self.data = delete_major(major_id)
def read_major(): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select m.major_id, major_name, count(student_id) as number_of_students ' \ 'from major as m left outer join student as s ' \ 'on m.major_id = s.major_id ' \ 'group by m.major_id ' \ 'order by m.major_id;' cursor.execute(sql) result = cursor.fetchall() data['majors'] = [] for row in result: tmp = {'major_id': row[0], 'major_name': row[1], 'number_of_students': row[2]} data['majors'].append(tmp) except Exception as e: data['error'] = str(e) finally: connection.close() return data
def insert_major(major_name): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: major_id = get_new_major_id() if major_id.get('error'): data['error'] = major_id['error'] return data sql = 'insert into major values(\'%s\', \'%s\');' % (major_id['major_id'], major_name) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def update_major(major_id, major_name): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'update major set major_name = \'%s\' where major_id = \'%s\';' % (major_name, major_id) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def insert_major_course(major_id, course_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'insert into major_course values(\'%s\', \'%s\');' % (major_id, course_id) cursor.execute(sql) """ sql = 'insert into student_course ( select student_id, \'%s\', ' \ 'null from student where major_id = \'%s\');' % (course_id, major_id) cursor.execute(sql) """ connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def insert_major_course(major_id, course_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'insert into major_course values(\'%s\', \'%s\');' % (major_id, course_id) cursor.execute(sql) """ sql = 'insert into student_course ( select student_id, \'%s\', ' \ 'null from student where major_id = \'%s\');' % (course_id, major_id) cursor.execute(sql) """ connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def insert_major_course(major_id, course_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'insert into major_course values(\'%s\', \'%s\');' % (major_id, course_id) cursor.execute(sql) """ sql = 'insert into student_course ( select student_id, \'%s\', ' \ 'null from student where major_id = \'%s\');' % (course_id, major_id) cursor.execute(sql) """ connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def delete_major_course(major_id, course_id): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: """ sql = 'delete from student_course ' \ 'where student_id in ( ' \ 'select student_id ' \ 'from student ' \ 'where major_id = \'%s\') ' \ 'and course_id = \'%s\'; ' % (major_id, course_id) cursor.execute(sql) """ sql = 'delete from major_course ' \ 'where major_id = \'%s\' and course_id = \'%s\' ' % (major_id, course_id) cursor.execute(sql) connection.commit() except Exception as e: data['error'] = str(e) connection.rollback() finally: connection.close() return data
def read_major_course(): connection = connect_to_sql() data = {} try: with connection.cursor() as cursor: sql = 'select m.major_id, major_name, c.course_id, course_name ' \ 'from major_course as mc, major as m, course as c ' \ 'where mc.major_id = m.major_id and mc.course_id = c.course_id ' \ 'order by m.major_id, c.course_id;' cursor.execute(sql) result = cursor.fetchall() data['major_courses'] = [] for row in result: tmp = {'major_id': row[0], 'major_name': row[1], 'course_id': row[2], 'course_name': row[3]} data['major_courses'].append(tmp) except Exception as e: data['error'] = str(e) finally: connection.close() return data
通过调试,对各个模块的功能进行测试;
本次实验我是在windows操作系统平台下完成的,应用了MySQL数据库建表,触发,删除插入.......;Python语言中的GUI图形用户界面、通过html+css+js技术进行网站搭建,使用postman等工具进行接口测试,与数据库连接〔jdbc〕等技术。较好的在规定时间内完成了老师安排给我们的任务,实验过程中尽管遇到了不少问题,但最后经过与同伴互相探讨,借阅资料,上网查阅等都使问题得到了解决。总的来说感觉数据库、flask、python语言等编程技术应用起来灵活,容易理解。历尽一个多月课程设计让我对python语言等编程技术产生了浓厚的兴趣,更重要的是增强了我学习的自信心。
我们做的题目是《学生成绩管理系统》按照老师的安排,我们先进行系统需求和系统功能设计,在确定了我们要做的学生成绩管理系统应具有哪些功能后利用visio汇出了管理系统的流程图,并且进一步利用visio的强大功能画出ER图继而生成了物理模型图以及数据库,完成了数据库设计阶段。〔包括需求分析和概要设计〕;在剩下来的几天里我们主要把精力集中在应用程序的调试及课程设计报告的书写上。通过网络、书籍等查找所需的资料。数据库局部的书写〔包括数据库、表、触发器、索引、存储过程等的创立〕以及完成ER图,物理模型图的设计转换局部。
心得与体会:
通过本次课程设计我深切的体会到学习与实践的重要性。第一、在实验的过程中我一边学习一边上机动手操作,才发现自己不懂的太多太多,以前的自己太渺小,我需要不断的在学习中成长。同时也深刻体会到自己需要在动手操作方面努力改良提高。第二、记得老师和学长们总是教导我们要学会请教别人,告诉我们请教别人能在走出社会以后更是显得尤为重要,之前我还有点不以为然,过分的相信自己。通过本次为期一个多月的课程设计我对老师和学长的话坚信不移,最后较好的完成了任务,也从中学到了不少东西。我笃信在以后的学习和成长过程中我会更加注重与他人协作,互助。
[1]蒋睿.MySQL数据库安全研究[J].电脑知识与技术,2020,16(09):3-4+21.
[2]王珊.数据库技术与应用[M].北京:清华大学出版社,2005.
[3]贺卉珍.基于配置管理数据库的高可用架构风险发现[J].中国金融电脑
,2020(03):68-74.
[4]郑智方,李彬,刘世坤,李鹏.探究mysql的运用实例-对数据库的宏观把握[J].科技风,2020(06):129.
[5]梁相栋,郭小燕,许亮,郑鑫伟.班级文化信息管理系统的建设与开发[J].甘肃科技,2020,36(04):25-27+39.
[6]杨琳,昌明权.基于MySQL的高校党支部信息管理系统数据库设计[J].电脑知识与技术,2020,16(05):276-277+284.
[7] 洪锦魁 .Python GUI 设计:tkinter 菜鸟编程. 清华大学出版社,2019.
[8] 张晓博.基于 Python 的 SQL Server 海量数据转移的研究与实现[J].铁路计算机应用,2012.
[9]萨师煊 , 王珊 . 数据库系统概论 [M]. 北京 : 高等教育出版社 ,2002.
[10] 乔晶 . 高校学生成绩管理系统设计与实现 [J]. 电脑编程技巧与维护 ,2015(23:59-60.
[11]周文艳.谈学生成绩管理系统中的数据库设计[J].沧州师范专科学校学报, 2006, 6, 22 (2) :65-67