├── .gitattributes ├── README.md ├── course_class.py ├── login_window.py ├── teacher_class.py ├── tets.py ├── 《数据库原理》报告最终.doc └── 根据书上表12.1添加S表信息.sql /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mysql_python_DB 2 | 基于mysql和python的学生选课系统 3 | -------------------------------------------------------------------------------- /course_class.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import tets 3 | 4 | class course: 5 | def __init__(self,SNO): 6 | self.root = Tk() 7 | self.root.wm_title(SNO) 8 | self.root.geometry("900x400") 9 | self.SNO=SNO 10 | self.student_label = Label(self.root, text="学生详细信息:") 11 | self.course_label = Label(self.root, text="可选课程") 12 | self.score_label = Label(self.root, text="已修课程成绩") 13 | self.choose_course_label = Label(self.root, text="已选课程") 14 | self.course_entry_label = Label(self.root, text="请输入课程号:") 15 | self.student_text = Text(self.root, height=10, width=50) 16 | self.course_text = Text(self.root, height=10, width=50) 17 | self.score_text = Text(self.root, height=10, width=50) 18 | self.choose_course_text = Text(self.root, height=10, width=50) 19 | self.course_entry = Entry(self.root, width=5) 20 | self.course_button = Button(self.root, text="选课", command=self.choose_course) 21 | self.course_button2 = Button(self.root, text="退课", command=self.delete_course) 22 | self.course_button3 = Button(self.root, text="关闭") 23 | def inilize(self): 24 | self.student_label.grid(row=0, column=0, sticky=W) 25 | self.student_text.grid(row=1, column=0, sticky=E) 26 | self.course_label.grid(row=0, column=1, sticky=W) 27 | self.course_text.grid(row=1, column=1, sticky=E) 28 | self.score_label.grid(row=3, column=0, sticky=W) 29 | self.score_text.grid(row=4, column=0, sticky=E) 30 | self.choose_course_label.grid(row=3, column=1, sticky=W) 31 | self.choose_course_text.grid(row=4, column=1, sticky=E) 32 | self.course_entry_label.grid(row=0, column=3) 33 | self.course_entry.grid(row=1, column=3) 34 | self.course_button.grid(row=1, column=4) 35 | self.course_button2.grid(row=2, column=4) 36 | self.course_button3.grid(row=4, column=4) 37 | 38 | # 选课 39 | def choose_course(self): 40 | course_number = self.course_entry.get() 41 | tets.insert_choose_course(self.SNO, str(course_number), 0) 42 | self.update_ui() 43 | 44 | # 退课 45 | def delete_course(self): 46 | course_number = self.course_entry.get() 47 | tets.delete_choose_course(str(course_number)) 48 | self.update_ui() 49 | 50 | # 更新ui 51 | def update_ui(self): 52 | self.student_text.delete(1.0, END) 53 | self.course_text.delete(1.0, END) 54 | self.score_text.delete(1.0, END) 55 | self.choose_course_text.delete(1.0, END) 56 | tets.display_student(self.student_text, self.SNO) 57 | tets.display_course(self.course_text, self.SNO) 58 | tets.display_score(self.score_text, self.SNO) 59 | tets.display_choose_course(self.choose_course_text, self.SNO) 60 | 61 | def start(self): 62 | self.inilize() 63 | self.update_ui() 64 | self.root.mainloop() 65 | 66 | 67 | if __name__=='__main__': 68 | c=course('S5') 69 | c.start() -------------------------------------------------------------------------------- /login_window.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import course_class 3 | import teacher_class 4 | import pymysql 5 | def judge(): 6 | login=user_entry.get() 7 | password=password_entry.get() 8 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student', charset='utf8') 9 | cursor = db.cursor() 10 | sql = "SELECT S.SNO FROM S where S.LOGN='%s' and S.PSWD='%s'" % (login,password) 11 | cursor.execute(sql) 12 | result = cursor.fetchall() 13 | if len(result)==0: 14 | cursor = db.cursor() 15 | sql = "SELECT T.TNAME FROM T where T.LOGN='%s' and T.PSWD='%s'" % (login, password) 16 | cursor.execute(sql) 17 | result = cursor.fetchall() 18 | else: 19 | return result 20 | return result 21 | 22 | def start(): 23 | 24 | result=judge() 25 | if len(result)!=0: 26 | root.destroy() 27 | if result[0][0][0]=='S': 28 | c=course_class.course(result[0][0]) 29 | c.start() 30 | else: 31 | t=teacher_class.Teacher(result[0][0]) 32 | t.start() 33 | else: 34 | Label(root, text="用户名或密码错误,请重新输入").grid(row=3, column=1) 35 | 36 | root = Tk() 37 | root.title("用户登录界面") 38 | root.geometry("300x200") 39 | 40 | Label(root,text="用户名").grid(row=0,column=0) 41 | user_entry=Entry(root,width=15) 42 | user_entry.grid(row=0,column=1) 43 | Label(root,text="密码").grid(row=1,column=0) 44 | password_entry=Entry(root,width=15) 45 | password_entry.grid(row=1,column=1) 46 | 47 | star_button=Button(root,text="登陆",command=start) 48 | star_button.grid(row=2,column=1) 49 | root.mainloop() -------------------------------------------------------------------------------- /teacher_class.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import tets 3 | 4 | class Teacher: 5 | def __init__(self,TNO): 6 | self.TNO=TNO 7 | self.root = Tk() 8 | self.root.title(TNO) 9 | self.root.geometry("900x400") 10 | self.course_label = Label(self.root, text="请选择课程名:") 11 | self.student_label = Label(self.root, text="已选修此课程的学生:") 12 | self.student_text = Text(self.root, width=20, height=10) 13 | self.lb = Listbox(self.root, width=20, height=10) 14 | Label(self.root,text="学号:").grid(row=3,column=3) 15 | Label(self.root, text="成绩:").grid(row=5, column=3) 16 | self.student_id_entry = Entry(self.root, text="学号",width=5) 17 | self.student_grape_entry = Entry(self.root, text="成绩",width=5) 18 | self.student_button = Button(self.root, text="查询", command=self.find_score) 19 | self.score_button =Button(self.root, text="输入成绩", command=self.change_score) 20 | 21 | def inilize(self): 22 | self.course_label.grid(row=1, column=0) 23 | self.student_label.grid(row=1, column=1) 24 | self.student_text.grid(row=2, column=1) 25 | var = StringVar() 26 | for item in tets.find_teacher_course(self.TNO): 27 | self.lb.insert(END, item) 28 | self.lb.grid(row=2, column=0) 29 | self.student_id_entry.grid(row=4, column=3) 30 | self.student_grape_entry.grid(row=6, column=3) 31 | # 查询 32 | self.student_button.grid(row=4, column=0) 33 | # 输入成绩 34 | self.score_button.grid(row=4, column=1) 35 | 36 | # 查询选了该老师该课的学生成绩 37 | def find_score(self): 38 | CNAME = self.lb.get(self.lb.curselection())[0] 39 | self.student_text.delete(1.0, END) 40 | tets.find_student_score(self.student_text, CNAME, self.TNO) 41 | #修改成绩 42 | def change_score(self): 43 | SNO = self.student_id_entry.get() 44 | GRADE = self.student_grape_entry.get() 45 | CNAME = self.lb.get(self.lb.curselection())[0] 46 | tets.change_score(SNO, GRADE, CNAME) 47 | self.find_score() 48 | 49 | def start(self): 50 | self.inilize() 51 | self.root.mainloop() 52 | 53 | if __name__=='__main__': 54 | c=Teacher('刘红') 55 | c.start() -------------------------------------------------------------------------------- /tets.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # coding=utf-8 3 | import pymysql 4 | import numpy as np 5 | from tkinter import * 6 | 7 | #选课 8 | def insert_choose_course(SNO,CNO,GRADE): 9 | db = pymysql.connect('localhost', 'root', 'huangxiao','student',charset='utf8') 10 | cursor = db.cursor() 11 | sql = "insert ignore SC (SNO,CNO,GRADE) values ('%s','%s',%d)" % (SNO,CNO,GRADE) 12 | cursor.execute(sql) 13 | db.commit() 14 | db.close() 15 | #退课 16 | def delete_choose_course(CNO): 17 | db = pymysql.connect('localhost', 'root', 'huangxiao','student',charset='utf8') 18 | cursor = db.cursor() 19 | sql = "delete from SC where CNO = '%s'" % CNO 20 | cursor.execute(sql) 21 | db.commit() 22 | db.close() 23 | 24 | #打印学生信息 25 | def display_student(text,LOGN): 26 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student',charset='utf8') 27 | cursor = db.cursor() 28 | sql = "SELECT * FROM S where S.LOGN='%s'"%LOGN 29 | cursor.execute(sql) 30 | col=[] 31 | for i in list(np.array(cursor.description)): 32 | col.append(i[0]) 33 | text.insert(END,'%s %4s %4s %4s %4s'%tuple(col[0:5])) 34 | text.insert(END,'\n') 35 | result = cursor.fetchall() 36 | 37 | for row in result: 38 | text.insert(END,'%s %4s %4s %4s %4s'%tuple(row[0:5])) 39 | text.insert(END,'\n') 40 | db.close() 41 | 42 | #显示可选课程 43 | def display_course(text,SNO): 44 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student',charset='utf8') 45 | cursor = db.cursor() 46 | sql = "select * from C where CNO not in (select C.CNO from S,SC,C where S.SNO=SC.SNO and C.CNO=SC.CNO and S.SNO='%s')"%SNO 47 | cursor.execute(sql) 48 | col=[] 49 | for i in list(np.array(cursor.description)): 50 | col.append(i[0]) 51 | text.insert(END,col) 52 | text.insert(END,'\n') 53 | 54 | result = cursor.fetchall() 55 | 56 | for row in result: 57 | text.insert(END,row) 58 | text.insert(END,'\n') 59 | db.close() 60 | 61 | #显示已选课程 62 | def display_choose_course(text,SNO): 63 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student',charset='utf8') 64 | cursor = db.cursor() 65 | sql = "select C.CNO,C.CNAME,C.CREDI,C.CDEPT,C.TNAME from S,SC,C where S.SNO=SC.SNO and C.CNO=SC.CNO and S.SNO='%s'" % SNO 66 | cursor.execute(sql) 67 | col=[] 68 | for i in list(np.array(cursor.description)): 69 | col.append(i[0]) 70 | text.insert(END,col) 71 | text.insert(END,'\n') 72 | 73 | result = cursor.fetchall() 74 | 75 | for row in result: 76 | text.insert(END,row) 77 | text.insert(END,'\n') 78 | db.close() 79 | 80 | #显示学生成绩 81 | def display_score(text,SNO): 82 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student',charset='utf8') 83 | cursor = db.cursor() 84 | sql = "select C.CNO,C.CNAME,GRADE from S,SC,C where S.SNO=SC.SNO and C.CNO=SC.CNO and S.SNO='%s'"%SNO 85 | cursor.execute(sql) 86 | col=[] 87 | for i in list(np.array(cursor.description)): 88 | col.append(i[0]) 89 | text.insert(END,col) 90 | text.insert(END,'\n') 91 | result = cursor.fetchall() 92 | 93 | for row in result: 94 | text.insert(END,row) 95 | text.insert(END,'\n') 96 | db.close() 97 | #老师所上的课程 98 | def find_teacher_course(name): 99 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student',charset='utf8') 100 | cursor = db.cursor() 101 | sql = "SELECT CNAME FROM C where TNAME='%s'" % name 102 | cursor.execute(sql) 103 | result = cursor.fetchall() 104 | db.close() 105 | return result 106 | 107 | #查询选了该老师该课的学生 108 | def find_student_score(text,CNAME,TNAME): 109 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student',charset='utf8') 110 | cursor = db.cursor() 111 | sql = "select S.SNO,S.SNAME,SC.GRADE from S,SC,C where S.SNO=SC.SNO and C.CNO=SC.CNO and C.CNAME='%s' and C.TNAME='%s'"%(CNAME,TNAME) 112 | cursor.execute(sql) 113 | col=[] 114 | for i in list(np.array(cursor.description)): 115 | col.append(i[0]) 116 | text.insert(END,col) 117 | text.insert(END,'\n') 118 | result = cursor.fetchall() 119 | 120 | for row in result: 121 | text.insert(END,row) 122 | text.insert(END,'\n') 123 | db.close() 124 | 125 | #老师修改成绩 126 | def change_score(SNO,GRADE,CNAME): 127 | db = pymysql.connect('localhost', 'root', 'huangxiao', 'student', charset='utf8') 128 | cursor = db.cursor() 129 | 130 | sql = "select CNO from C where CNAME='%s'" % (CNAME) 131 | cursor.execute(sql) 132 | result = cursor.fetchall() 133 | sql = "replace into SC (SNO,CNO,GRADE) values ('%s','%s',%d)" % (SNO, result[0][0], int(GRADE)) 134 | cursor.execute(sql) 135 | db.commit() 136 | db.close() 137 | 138 | 139 | -------------------------------------------------------------------------------- /《数据库原理》报告最终.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hlilyjiaxin/mysql_python_DB/a202875cf3d9c37660170450122f47d3cc4325af/《数据库原理》报告最终.doc -------------------------------------------------------------------------------- /根据书上表12.1添加S表信息.sql: -------------------------------------------------------------------------------- 1 | // 建表 2 | use student 3 | create table S(SNO char(4),SNAME char(8),SEX char(2),AGE char(2),SDEPT char(10),LOGN char(20),PSWD char(20), primary key(SNO)) default charset utf8; 4 | create table C(CNO char(4),CNAME char(20),CREDI int(11),CDEPT char(10),TNAME char(8), primary key(CNO)) default charset utf8; 5 | create table SC(SNO char(4),CNO char(4),GRADE int(11), primary key(SNO, CNO), foreign key(CNO)references C(CNO), foreign key(SNO)references S(SNO)) default charset utf8; 6 | 7 | insert into S values('S1','李铭','男','19','计算机软件','S1','S1'); 8 | insert into S values('S2','刘晓鸣','男','20','计算机应用','S2','S2'); 9 | insert into S values('S3','李明','男','22','计算机应用','S3','S3'); 10 | insert into S values('S4','张鹰','女','21','计算机软件','S4','S4'); 11 | insert into S values('S5','刘竟静','女','22','计算机软件','S5','S5'); 12 | insert into S values('S6','刘成刚','男','21','计算机软件','S6','S6'); 13 | insert into S values('S7','王铭','男','22','计算机应用','S7','S7'); 14 | insert into S values('S8','宣明尼','女','18','计算机应用','S8','S8'); 15 | insert into S values('S9','柳红利','女','19','计算机软件','S9','S9'); 16 | 17 | insert into C values('C1', 'PASCAL', 4, '计算机应用', '王晓名'); 18 | insert into C values('C2', '数据结构', 4, '计算机应用', '刘红'); 19 | insert into C values('C3', '离散数学', 4, '计算机应用', '李严劲'); 20 | insert into C values('C4', '计算机原理', 6, '计算机软件', '王晓名'); 21 | insert into C values('C5', '数据库原理', 4, '计算机应用', '吴志刚'); 22 | insert into C values('C6', 'Windows技术', 4, '计算机软件', '吴志刚'); 23 | insert into C values('C7', '编译原理', 4, '计算机软件', '蒋莹岳'); 24 | insert into C values('C8', '系统结构', 6, '计算机应用', '刘红'); 25 | 26 | insert into SC values('S1', 'C2', 56); 27 | insert into SC values('S1', 'C4', 78); 28 | insert into SC values('S1', 'C6', 66); 29 | insert into SC values('S1', 'C8', 88); 30 | insert into SC values('S3', 'C1', 88); 31 | insert into SC values('S3', 'C2', 76); 32 | insert into SC values('S4', 'C1', 67); 33 | insert into SC values('S4', 'C2', 76); 34 | insert into SC values('S4', 'C3', 67); 35 | insert into SC values('S5', 'C1', 67); 36 | insert into SC values('S5', 'C2', 78); 37 | insert into SC values('S5', 'C3', 91); 38 | insert into SC values('S6', 'C1', 78); 39 | --------------------------------------------------------------------------------