Python+Mysql学⽣选课系统(附下载链接)
程序在python3.5.2下调试通过
mysql发⾏版本:5.7.24
法律允许范围内,作者保留所有权利!
项⽬相关⽂件可以在百度⽹盘下载,地址为
代码结构图:
数据库结构图:
下⾯是创建数据库的命令:
#bash下登陆数据库
mysql -u root -p
#创建并进⼊数据库
create database student_info default charset=utf8;
use student_info;
#创建学⽣信息存储表
#这张表以学⽣id为主键,学⽣姓名为字符类型,固定长度10位,默认字符集utf8
create table student(id int primary key, name char(10) not null) default charset=utf8;
#创建课程信息存储表
#这张表以课程id为主键,并有⾃增属性,课程名称为变长,最长30位,不能为空;默认字符集utf8
create table course(courseid int primary key auto_increment, coursename varchar(30) not null) default charset=utf8;
#创建中间表
#存储哪位学⽣选了哪个课程
#学⽣id为主键,为了加快检索速度,该字段以student表中的id为外键
#courseid字段以course表中的id为外键
create table selection(studentid int not null, courseid int not null unique, foreign key(studentid) references student(id) on delete cascade on update cascade, fore
#创建学⽣的⽤户名密码表
#包含username⽤户名和passwd密码
create table user(username int, passwd char(20) not null, foreign key(username) references student(id) on update cascade on delete cascade);mysql下载要钱吗
#创建管理员数据表
create table admin(username char(20) not null, passwd char(20) not null);
#向⽤户名密码表中添加admin-admin
#⽤户注册使可以向该张表中加⼊数据,以便验证
insert into admin values("admin","admin");
下⾯是数据库操作模块代码:
# -*- coding: UTF-8 -*-
import pymysql
class mysql():
def __init__(self, address, port, user, passwd, char_set):
"""
Function: 类的初始化函数,数据库对象mysql在初始化时直接连接数据库,并获取cursor对象
args:
self:类对象
address:要连接的数据库ip地址
port:要连接的数据库端⼝
user:连接数据库⽤户名
passwd:连接数据库密码
passwd:连接数据库密码
char_set:连接⽤字符集,⼀般为"utf8"(默认)
return: None
"""
try:
self.database = t(address, user, passwd, charset=char_set, port = port)  self.cursor = self.database.cursor()
ute("use student_info")
except Exception as e:
print("Connection failed!")
print(e)
exit(-1)
def is_admin(self, admin_name):
"""
func:判断⽤户名admin_name是不是管理员账户
args:
admin_name:需要判断的账户名
return:
True/False
"""
if ute("select * from admin where username=\"%s\"" % admin_name) == 0:  return False
return True
def is_admin_passwd_correct(self, username, passwd):
"""
func:判断⽤户名username的密码是否为passwd
args:
admin_name:需要判断的账户名
passwd:需要判断的密码
return:
True/False
"""
ute("select * from admin where username=\"%s\"" % username)
if self.cursor.fetchall()[0][1] == passwd:
return True
return False
@staticmethod
def username2int(input_username_str):
"""
func:把⾮管理员类的⽤户名,即学⽣学号,由字符串转为int
args:
input_username_str:需要转换的⽤户名
return:
int
"""
try:
input_username_int = int(input_username_str)
except:
return None
return input_username_int
@staticmethod
def is_empty_input(str1, str2):
"""
func:判断输⼊字符串是否为空
args:
两个需要判断的字符串
return:
bool
"""
if (str1 == "") or (str2 == ""):
if (str1 == "") or (str2 == ""):
return True
return False
def is_student_exist(self,username):
"""
func:通过学号判断学⽣是否存在
args:
username:需要判断的学号
return:
bool
"""
if ute("select * from user where username=%d" % username) != 0:
return True
return False
def is_student_passwd_correct(self, username, passwd):
"""
func:判断学⽣的⽤户名及密码是否匹配
args:
username:需要判断的学号
passwd:需要判断的密码
return:
bool
"""
if ute("select * from user where username=%d and passwd='%s'" % (username, passwd)) != 0:
return True
return False
def get_course_list(self):
"""
func:得到现有的课程列表
args:
return:
包含了课程列表的元组,格式为
((课程id1,课程名1),(课程id2,课程名2)......)
"""
ute("select * from course")
return self.cursor.fetchall()
def course_exist(self, courseid):
"""
func:查看课程是否存在
args:
courseid:要查看的课程id
return:
bool
"""
if ute("select * from course where courseid = %d" % courseid) != 0:
return True
return False
def course_have_been_selected(self, studentid, courseid):
"""
func:查询某位学⽣是否已经选择了某课程
args:
studentid:要查看的学⽣id
courseid:要查看的课程id
return:
bool
"""
if ute("select * from selection where studentid = %d and courseid = %d" % (studentid, courseid)) != 0:  return True
return False
def select(self, studentid, courseid):
"""
func:某位学⽣选课
args:
studentid:要选课的学⽣id
courseid:要选择的课程id
return:
None
"""
ute("insert into selection values(%d,%d)" % (studentid, courseid))
self.databasemit()
def get_student_course(self, studentid):
"""
func:获取某位同学已经选择的课程
args:
studentid:要查看的学⽣id
return:
包含课程信息的列表,元素均为字符串
[课程名1,课程名2...]
"""
ute("select * from selection where studentid=%d" % studentid)
t = list()
for i in self.cursor.fetchall():
ute("select coursename from course where courseid=%d" % i[1])
t.append((i[1], self.cursor.fetchall()[0][0]))
return t
def delete_course_from_student(self, studentid, courseid):
"""
func:在某位学⽣的选课表中删除⼀门课程
args:
studentid:要删除课程的学⽣id
courseid:要删除的课程id
return:
None
"""
ute("delete from selection where studentid = %d and courseid = %d" % (studentid, courseid))  self.databasemit()
def get_student_list(self):
"""
func:获得所有学⽣id及其姓名
args:
return:
⼀个元组,结构为
((学⽣1id,学⽣1姓名),(学⽣2id,学⽣2姓名)...)
"""
ute("select * from student")
return self.cursor.fetchall()
def get_student_name(self, studentid):
"""
func:从学⽣id获得学⽣姓名
args:
studentid:学⽣id
return:
str(学⽣姓名)
"""
ute("select * from student where id=%d" % studentid)
return self.cursor.fetchall()[0][1]
def add_student(self, studid, studname):

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。