Python+Mysql实现教务管理系统
需求:设计一款教务管理系统,实现如下功能:1、此教务管理系统面向三种身份,学生、教职工、系统管理员2、要有登录界面,在登录界面能根据不同身份自动选择登录入口3、教职工凭借工号和密码登录,学生凭借学号和密码登录,系统管理员设置特定帐号和密码4、教职工(1)教职工基本属性:姓名 性别 出生日期 职称 工资 工号 登录密码(2)教职工的功能:修改个人信息、查看个人信息、浏览高级/低级消息、申请发布低级消
需求:
设计一款教务管理系统,实现如下功能:
1、此教务管理系统面向三种身份,学生、教职工、系统管理员
2、要有登录界面,在登录界面能根据不同身份自动选择登录入口
3、教职工凭借工号和密码登录,学生凭借学号和密码登录,系统管理员设置特定帐号和密码
4、教职工
(1)教职工基本属性:姓名 性别 出生日期 职称 工资 工号 登录密码
(2)教职工的功能:修改个人信息、查看个人信息、浏览高级/低级消息、申请发布低级消息、管理自己发布的低级消息
5、学生
(1)学生的基本属性:姓名 性别 出生日期 学院 班主任 专业 学号 登录密码 级段
(2)学生的功能:修改个人信息、查看个人信息、浏览二级消息
6、系统管理员
(1)系统管理员的基本属性:帐号 登录密码
(2)系统管理员的功能:注册教职工、注册学生、管理所有教职工\学生信息、发布高级级消息、管理所有消息、审核低级消息发布申请,初始化教职工和学生密码
简单设计:
1、在MySQL数据库中创建数据库:test
2、创建表:LoginAccount StudentInfo TeacherInfo AllMessage DefaultPassword PositionList
3、LoginAccount:Id Account Password AccountLevel
Id是账户序号
Account对于系统管理员是帐号,对于教职工是工号,对于学生是学号
Password密码
AccountLevel账户级别 0系统管理员 1教职工 2学生
4、StudentInfo: Id Name Gender Birth Academy TeacherNo Major StudentNo(长度固定为12) EntryDate
TeacherId: 班主任的工号
EntryDate:入学时间
5、TeacherInfo:Id Name Gender Birth PositionNo Salary TeacherNo(长度固定为5) EntryDate
PositionNo: 职务编号
EntryDate: 入职时间
6、AllMessage:Id MsgLevel SenderNo SenderName SendTime Title Content Statu
Statu消息状态 wait等待审核 pass审核通过 fail审核未通过
MsgLevel 0高级消息 1低级消息
7、登录界面:
(1)登录入口设置为三个:管理员入口 教职工入口 学生入口
(2)在登录时,检验密码和账户是否匹配,然后再检验账户级别和所选登录入口是否匹配,都匹配则登录成功,反之,登录失败
8、学生界面:
(1)设置功能菜单:个人信息<查看,修改> 消息
9、教职工界面:
(1)设置功能菜单:个人信息<查看,修改> 消息<消息列表,发布消息,我的消息<删除> >
10、管理员界面:
(1)设置功能菜单:教职工<浏览,注册,修改,删除,初始化密码> 学生<浏览,注册,修改,删除,初始化密码> 消息<消息列表,发布消息,审核消息,删除消息>
11、DefaultPassword:Id AccountLevel Password
各个级别的初始化密码
12、PositionList:Id PositionNo PositionName
职务列表,PositionNo职务编号 PositionName职务名称
在Scripts文件目录下打开cmd,输入命令 pip install mysqlclient 连接数据库,数据库连接完成以后打开mysql,source+空格+导入的数据库(用鼠标拖入),导入数据库,show databases->use+导入的数据库名->show tables from 导入的数据库名->select * from 数据库中的表名 查询完成后就可以在Python中运行主函数,进行教务系统管理了
类设计:
主函数
import os
import MySQLdb
import Student
import Teacher
import Login
import SystemManager
if __name__ == '__main__':
conn = MySQLdb.connect(user='root',password = '123456',db = 'student2021')
log = Login.Login(conn)
if log.MainFunc():
account = log.GetLoginAccount()
#print('AAAAAAAAAAAA',account[0])
if account[2] == 0:
usr = SystemManager.SystemManager(conn,account[0],account[1])
usr.MainFunc()
elif account[2] == 1:
usr = Teacher.Teacher(conn,account[0],account[1])
usr.MainFunc()
elif account[2] == 2:
usr = Student.Student(conn,account[0],account[1])
usr.MainFunc()
else :
conn.close()
raise exception()
conn.close()
1、class Login 登录界面
def __init__(self,conn) 构造,conn连接数据库
def MainFunc(self) 主要执行函数
def CheckAccount(self) 检查账户
def LoginSurface(self,info) 登录界面,info相关信息[错误信息]
def GetLoginAccount(self) 获取登录的帐号和密码和权限
def Quit(self) 退出
import os
import MySQLdb
import time
class Login:
def __init__(self,conn):
self.account = ''
self.password = ''
self.level = 2
self.conn = conn
def LoginSurface(self,info):
os.system('cls')
width = 50
title = 'LOGIN'
body1 = '[A]Admin'
body2 = '[T]Teacher'
body3 = '[S]Student'
body4 = '[Q]Quit'
print('=' * width)
#print(' ' * ((width-len(title))/2),end='')
print(title)
#print( ' ' * ((width-len(body1))/2),end='')
print(body1)
#print(' ' * ((width-len(body1))/2),end='')
print(body2)
#print(' ' * ((width-len(body1))/2),end='')
print(body3)
#print(' ' * ((width-len(body1))/2),end='')
print(body4)
#print(' ' * ((width-len(info))/2),end='')
print(info)
print('-' * width)
def MainFunc(self):
err = ''
while True:
self.LoginSurface(err)
level =input('Access:')
level = level.upper()
if level == 'A':self.level = 0
elif level == 'T': self.level = 1
elif level == 'S': self.level = 2
elif level =='Q': return False
else :
err = 'Error Action!'
continue
self.account = input('Account:')
self.password = input('Password:')
if self.CheckAccount():
err = 'Login Success!'
self.LoginSurface(err)
print('Please wait...')
time.sleep(3)
return True;
else :
err = 'Login Failed!'
def GetLoginAccount(self):
return [self.account,self.password,self.level]
def CheckAccount(self):
cur = self.conn.cursor()
sqlcmd = "select Account,Password,AccountLevel from LoginAccount where Account = '%s'" % self.account
if cur.execute(sqlcmd) == 0: return False
temp = cur.fetchone()
cur.close()
if temp[1] == self.password and temp[2] == self.level:
return True
else: return False
def Quit(self):
pass
if __name__ == '__main__':
conn = MySQLdb.connect(user='root',passwd = '123456',db = 'test2020');
a = Login(conn)
a.MainFunc()
a.Quit()
conn.close()
2、class Student 学生
def __init__(self,conn,account,passwd) 构造,conn连接数据库,account当前学生的帐号
def MainFunc(self) 主要执行函数
def PersonalInfo(self) 个人信息
def ChangePersonalInfo(self) 修改个人信息
def OperatMessage(self) 操作消息
def MessageList(self) 查看消息列表
def MessageInfo(self,MsgNo) 查看详细消息, No消息编号
def Quit(self) 退出
def MainSurface(self,info) 主界面
def MessageSurface(self,info) 消息界面
def PersonalInfoSurface(self,info) 个人信息界面
import MySQLdb
import os
class Student:
def __init__(self,conn,account,passwd):
###构造,conn连接数据库
cur = conn.cursor()
sqlcmd = "select Name,Gender,Birth,Academy,Major,Grade,TeacherNo from studentinfo where StudentNo = '%s'" % account
cur.execute(sqlcmd)
res = cur.fetchone()
#print(res)
#print('!!!!')
sqlcmd = "select Name from studentinfo where TeacherNo = '%s'" % res[6]
cur.execute(sqlcmd)
TeacherName = cur.fetchone()
cur.close()
self.width = 150
self.conn = conn
self.account = account
self.Password= passwd
self.Name = res[0]
self.Gender = res[1]
self.Birth = res[2]
self.Academy= res[3]
self.Major = res[4]
self.Grade = res[5]
self.Teacher = TeacherName[0]
def MainFunc(self):
###主要执行函数
info = ''
while True:
self.MainSurface(info)
choice = input('What to do?')
choice = choice.upper()
if choice != 'P' and choice != 'M' and choice != 'Q' and choice != 'L'and choice != 'S':
info = 'Error Action!'
continue
if choice == 'P':
info = self.PersonalInfo()
elif choice == 'M':
info = self.OperatMessage()
elif choice == 'L':
info = self.ChooseLessonInfo()
elif choice == 'S':
info = self.stchoose()
else : break
def PersonalInfo(self):
###个人信息
info = ''
while True:
self.PersonalInfoSurface(info)
choice =input('What to do?')
choice = choice.upper()
if choice != 'C' and choice != 'Q':
info = 'Error Action!'
continue
if choice == 'C':
info = self.ChangePersonalInfo()
else : break
return info
def ChangePersonalInfo(self):
###修改个人信息
NewGender = self.Gender
NewBirth = self.Birth
NewPw = self.Password
##########
print('=' * self.width)
print('ChangePersonalInfo')
bd0='[G]Gender'
bd1='[B]Birth'
bd2='[P]Password'
bd3='[Q]Gender'
print(bd0)
print(bd1)
print(bd2)
print(bd3)
choice = input('What to do?')
choice = choice.upper()
if choice != 'G' and choice != 'B' and choice != 'P' and choice != 'Q':
info = 'Error Action!'
#continue
if choice == 'G':
while True:
choice = input('Change Gender?(y/n)')
choice = choice.lower()
if choice == 'y':
NewGender =input('New Gender:')
break
elif choice == 'n': break
else : pass
elif choice == 'B':
while True:
choice =input('change Born Date?(y/n)')
choice = choice.lower()
if choice == 'y':
NewBirth =input('New Born Date:')
break
elif choice == 'n': break
else : pass
elif choice == 'P':
while True:
choice =input('change Password?(y/n)')
choice = choice.lower()
if choice == 'y':
NewPw =input('New Password:')
break
elif choice == 'n': break
else : pass
#else : break
##########'''
'''while True:
choice = input('Change Gender?(y/n)')
choice = choice.lower()
if choice == 'y':
NewGender =input('New Gender:')
break
elif choice == 'n': break
else : pass
while True:
choice =input('change Born Date?(y/n)')
choice = choice.lower()
if choice == 'y':
NewBirth =input('New Born Date:')
break
elif choice == 'n': break
else : pass
while True:
choice =input('change Password?(y/n)')
choice = choice.lower()
if choice == 'y':
NewPw =input('New Password:')
break
elif choice == 'n': break
else : pass'''
info = 'Change Success!'
cur = self.conn.cursor()
if NewGender != self.Gender or NewBirth != self.Birth:
sqlcmd = "update StudentInfo set Gender = '%s',Birth = '%s' where StudentNo = '%s'" % (NewGender,NewBirth,self.account)
if cur.execute(sqlcmd) == 0:
self.conn.rollback()
cur.close()
return 'Change Fail!'
if NewPw != self.Password:
sqlcmd = "update LoginAccount set Password = '%s' where Account='%s'" % (NewPw,self.account)
if cur.execute(sqlcmd) == 0:
self.conn.rollback()
cur.close()
return 'Change Fail!'
else :
self.conn.commit()
self.Gender = NewGender
self.Birth = NewBirth
self.Password = NewPw
cur.close()
return 'Change Success!'
def OperatMessage(self):
info = ''
while True:
self.MessageSurface(info)
self.MessageList()
choice =input('What to do?')
choice = choice.upper()
if choice == 'M':
msg = input('Message Id:')
info = self.MessageInfo(msg)
elif choice == 'Q': break;
else : info = 'Error Action!'
return info
def MessageList(self):
###查看消息列表
cur = self.conn.cursor()
print()
sqlcmd = "select Id,SenderName,SendTime,Title from AllMessage where statu = 'pass' and MsgLevel = 1"
if cur.execute(sqlcmd) == 0: return
print('-' * self.width)
while True:
temp = cur.fetchone()
if not temp: break;
print('%3d%-20s%-50s%s' % (temp[0],temp[1],temp[3],temp[2]))
print('-' * self.width)
cur.close()
def MessageInfo(self,MsgNo):
###查看详细消息, No消息编号
cur = self.conn.cursor()
sqlcmd = "select SenderName,SendTime,Title,Content from AllMessage where Id = %s" % MsgNo
if cur.execute(sqlcmd) == 0:
cur.close()
return 'Read Fail!'
article = cur.fetchone()
cur.close()
os.system('cls')
print('*.' * self.width)
print(article[2])
head = article[0] + ' ' + str(article[1])
print( head)
print ('-' * self.width)
print(article[3])
print('*' * self.width)
input('Press any key to return!')
return ''
def ChooseLessonInfo(self):
####选课操作
cur = self.conn.cursor()
sqlcmd = "select * from lessoninfo"
cur.execute(sqlcmd)
print('%10s|%10s|%10s|%20s|%8s' % ('Lesson No','LessonName','Teacher No','Date','ClassRoom'))
while True:
res = cur.fetchone()
if not res: break
print('%10s|%10s|%10s|%20s|%8s' % (res[0],res[1],res[2],res[3],res[4]))
print('-' * self.width)
Cno =input('enter class number:')
#print(cur.execute("select lesNo from lessoninfo where lesNo = '%s'" % Cno))
if cur.execute("select lesNo from lessoninfo where lesNo = '%s'" % Cno) == 0:
cur.close()
return 'No Selected Class'
#print(self.account)
#print(Cno)
sqlcmd = "select * from lessoninfo where LesNo= '%s'" % Cno
cur.execute(sqlcmd)
res = cur.fetchone()
print(res[0])
print(res[1])
print(res[2])
print(res[3])
print(res[4])
sqlcmd = "insert into stchoose(studentno,lesNo,LesName,date,classroom) values('%s','%s','%s','%s','%s')" % (self.account,res[0],res[1],res[3],res[4])
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Choose Class Successfully!'
def stchoose(self):
#查看选课表
cur = self.conn.cursor()
while True:
print('Your Lessons :')
sqlcmd = "select * from stchoose where studentno = '%s' " % self.account
cur.execute(sqlcmd)
#cone = cur.fetchone()
#print(cone)
print('%10s|%20s|%25s|%15s|' % ('LesNo','lessonName','Time','ClassRoom'))
while True:
res = cur.fetchone()
if not res: break
print('%10s|%20s|%25s|%15s' % (res[1],res[2],res[3],res[4]))
print('-' * self.width)
body1 = '[S]Search Score'
body2 = '[Q]Quit'
print(body1)
print(body2)
print('-' * self.width)
choice =input('What to do?')
choice = choice.upper()
if choice == 'S':
cur = self.conn.cursor()
Cno =input('enter class number:')
#sqlcmd = "select * from lessoninfo where lesNo = '%s'" % cond[1]
#cur.execute(sqlcmd)
#res = cur.fetchone()
#info = self.seacerchscore(res[0],res[3])
info = self.seacerchscore(Cno,self.account)
elif choice == 'Q': break;
else : info = 'Error Action!'
self.conn.commit()
cur.close()
def seacerchscore(self,lNo,sNo):
cur = self.conn.cursor()
sqlcmd = "select lesName from lessoninfo where lesNo = '%s'" % lNo
cur.execute(sqlcmd)
lname = cur.fetchone()
print('-' * self.width)
print('StudentNo :%s' % sNo)
print('lesson :%s' % lname)
sqlcmd = "select score from stchoose where lesNo = '%s' and studentno = '%s'" % (lNo,sNo)
cur.execute(sqlcmd)
scor = cur.fetchone()
print('score : %s' % scor)
print('-' * self.width)
self.conn.commit()
cur.close()
while True:
choice =input('[S]Sure')
choice = choice.upper()
if choice == 'S': break;
else : info = 'Error Action!'
print('-' * self.width)
def Quit(self):
###退出
pass
def MainSurface(self,info):
###主界面
os.system('cls')
print('*' * self.width)
title = 'Welcome %s!' % self.Name
body1 = '[P]Personal Information'
body2 = '[M]Message'
body4 = '[L]Lessons Choose'
body5 = '[S]Stchoose'
body3 = '[Q]Quit'
print(title)
print(body1)
print(body2)
print(body4)
print(body5)
print(body3)
print(info)
print('=' * self.width)
def MessageSurface(self,info):
###消息界面
os.system('cls')
print('=' * self.width)
title = 'MESSAGES'
body1 = '[M]Message Detail'
body2 = '[Q]Quit'
# ' ' * ((self.width - len(title))/2),
print(title)
print(body1)
print(body2)
#print(' ' * ((self.width - len(body1)/2),body1)
#print(' ' * ((self.width - len(body2))/2),body2)
#print(' ' * ((self.width - len(info))/2),info)
print(info)
print('=' * self.width)
def PersonalInfoSurface(self,info):
###个人信息界面
os.system('cls')
print('*' * self.width)
title = 'PERSONAL INFORMATION'
body1 = '[C]Change Information'
body2 = '[Q]Quit'
print(title)
print(body1)
print(body2)
print (info)
print('-' * self.width)
body3 = ' Name: %s' % self.Name
body4 = 'Student Number: %s' % self.account
body5 = ' Gender: %s' % self.Gender
body6 = ' Birth: %s' % self.Birth
body7 = ' Academy: %s' % self.Academy
body8 = ' Major: %s' % self.Major
body9 = ' Grade: %s' % self.Grade
body10= ' Teacher: %s' % self.Teacher
print (body3)
print (body4)
print (body5)
print (body6)
print (body7)
print (body8)
print(body9)
print (body10)
print( '=' * self.width)
if __name__ == '__main__':
conn = MySQLdb.connect(user='root',passwd = 'root',db = 'student2021')
#stu = Student(conn,'0000001','123456')
stu = Student(conn,'202001','123456')
stu.MainFunc()
conn.close()
3、class Teacher 教师
def __init__(self,conn,account,passwd) 构造,conn连接数据库,account当前教职工帐号
def MainFunc(self) 主要执行函数
def OperatePersonalInfo(self) 操作个人信息
def ChangePersonalInfo(self) 修改个人信息
def MessageList(self) 查看消息列表
def MessageInfo(self,MsgNo) 查看详细消息, MsgNo消息编号
def CreateMessage(self) 发布消息
def OperateMessage(self) 操作个人消息
def PersonalMessage(self) 查看个人消息
def DeleteMessage(self) 删除个人消息
def MainSurface(self,info) 主界面
def PersonalInfoSurface(self,info) 个人信息界面
def MessageSurface(self,info) 消息界面
def PersonalMessageSurface(self,info) 个人消息界面
import os
import MySQLdb
class Teacher:
def __init__(self,conn,account,passwd):
cur = conn.cursor()
sqlcmd = "select Name,TeacherNo,Gender,Birth,PositionNo,Salary from TeacherInfo where TeacherNo = '%s'" % account
cur.execute(sqlcmd)
temp = cur.fetchone()
sqlcmd = "select PositionName from PositionList where PositionNo = '%s'" % temp[4]
cur.execute(sqlcmd)
pos = cur.fetchone()
cur.close()
self.PositionName = pos[0]
self.width = 150
self.conn = conn
self.Name = temp[0]
self.TeacherNo = temp[1]
self.Gender = temp[2]
self.Birth = temp[3]
self.PositionNo = temp[4]
self.Salary = temp[5]
self.Password = passwd
def MainFunc(self):
####主要执行函数
info = ''
while True:
self.MainSurface(info)
choice =input('What to do?')
choice = choice.upper()
if choice == 'P':
info = self.OperatePersonalInfo()
elif choice == 'M':
info = self.OperateMessage()
elif choice == 'S':
info = self.ScoreMark()
elif choice == 'Q': break
else : info = 'Error Action'
def ScoreMark(self):
###打分
cur = self.conn.cursor()
sqlcmd = "select * from lessoninfo"
cur.execute(sqlcmd)
print('%10s|%10s|%10s|%20s|%8s' % ('Lesson No','LessonName','Teacher No','Date','ClassRoom'))
while True:
res = cur.fetchone()
if not res: break
print('%10s|%10s|%10s|%20s|%8s' % (res[0],res[1],res[2],res[3],res[4]))
print('-' * self.width)
Cno =input('enter class number :')
Sno =input('enter student number :')
if cur.execute("select lesNo from stchoose where lesNo = '%s' and StudentNo = '%s'" % (Cno,Sno)) == 0:
cur.close()
return 'No Selected Class'
Sc =input('enter score :')
sqlcmd = "update stchoose set score = %s where lesNo = '%s' and StudentNo = '%s'" % (Sc,Cno,Sno)
cur.execute(sqlcmd)
print('Successfully!!!')
self.conn.commit()
cur.close()
def OperatePersonalInfo(self):
####操作个人信息
info = ''
while True:
self.PersonalInfoSurface(info)
choice =input('What to do?')
choice = choice.upper()
if choice == 'C':
info = self.ChangePersonalInfo()
elif choice == 'Q': break
else : info = 'Error Action'
return info
def ChangePersonalInfo(self):
####修改个人信息
NewGender = self.Gender
NewBirth = self.Birth
NewPw = self.Password
cur = self.conn.cursor()
while True:
choice =input('Change Gender?(y/n)')
choice = choice.lower()
if choice == 'y':
NewGender =input('New Gender:')
break
elif choice == 'n': break
else : pass
while True:
choice =input('Change Born Data?(y/n)')
choice = choice.lower()
if choice == 'y':
NewBirth =input('New Born Date:')
break
elif choice == 'n': break
else : pass
while True:
choice =input('Change Password?(y/n)')
choice = choice.lower()
if choice == 'y':
NewPw =input('New Password:')
break
elif choice == 'n': break
else :pass
if NewBirth != self.Birth or NewGender != self.Gender:
sqlcmd = "update TeacherInfo set Birth='%s',Gender='%s' where TeacherNo = '%s'" % (NewBirth,NewGender,self.TeacherNo)
if 0 == cur.execute(sqlcmd):
self.conn.rollback()
cur.close()
return 'Changer Fail'
if NewPw != self.Password:
sqlcmd = "update LoginAccount set Password='%s' where Account='%s'" % (NewPw,self.TeacherNo)
if 0 == cur.execute(sqlcmd):
self.conn.rollback()
cur.close()
return 'Change Fail!'
else :
self.conn.commit()
self.Gender = NewGender
self.Password = NewPw
self.Birth = NewBirth
cur.close()
return 'Change Success!'
def MessageList(self):
#####查看消息列表
cur = self.conn.cursor()
print()
sqlcmd = "select Id,SenderName,SendTime,Title from AllMessage where statu = 'pass' and MsgLevel <= 1"
if cur.execute(sqlcmd) == 0: return
print('-' * self.width)
while True:
temp = cur.fetchone()
if not temp: break;
print('%3d%-20s%-50s%s' % (temp[0],temp[1],temp[3],temp[2]))
print('-' * self.width)
cur.close()
def MessageInfo(self,MsgNo):
####查看详细消息, MsgNo消息编号
cur = self.conn.cursor()
sqlcmd = "select SenderName,SendTime,Title,Content from AllMessage where Id = %d" % MsgNo
if cur.execute(sqlcmd) == 0:
cur.close()
return 'Read Fail!'
article = cur.fetchone()
cur.close()
os.system('cls')
print('=' * self.width)
print(' ' * ((self.width - len(article[2]))/2) , article[2])
head = article[0] + ' ' + str(article[1])
print(' ' * ((self.width - len(head))/2) , head)
print('-' * self.width)
print(article[3])
print('=' * self.width)
input('Press any key to return!')
return ''
def CreateMessage(self):
####发布消息
print()
print(' Publish Messsage')
title =input('Message Title:')
path =input('Message Path:')
fp = open(path,'r')
body = fp.read()
fp.close()
sqlcmd = "insert into AllMessage(MsgLevel,SenderNo,SenderName,SendTime,Title,Content,statu) values(1,'%s','%s',now(),'%s','%s','wait')" % (self.TeacherNo,self.Name,title,body)
cur = self.conn.cursor()
info = 'Publish Success!'
if 0 == cur.execute(sqlcmd):
info = 'Publish Fail'
self.conn.rollback()
else:
self.conn.commit()
cur.close()
return info
def OperateMessage(self):
#####管理消息
info = ''
while True:
self.MessageSurface(info)
self.MessageList()
choice =input('What to do?')
choice = choice.upper()
if choice == 'P':
info = self.CreateMessage()
elif choice == 'Y':
info = self.PersonalMessage()
elif choice == 'M':
msg = input('Message Id:')
info = self.MessageInfo(msg)
elif choice == 'Q': break
else : info = 'Error Action'
return info
def PersonalMessageList(self):
cur = self.conn.cursor()
sqlcmd = "select Id,SenderName,SendTime,Title from AllMessage where SenderNo='%s'" % self.TeacherNo
if cur.execute(sqlcmd) != 0:
print('-' * self.width)
while True:
temp = cur.fetchone()
if not temp: break;
print('%3d%-20s%-50s%s' % (temp[0],temp[1],temp[3],temp[2]))
print('-' * self.width)
cur.close()
def PersonalMessage(self):
#####查看个人消息
info = ''
while True:
self.PersonalMessageSurface(info)
self.PersonalMessageList()
choice =input('What to do?')
choice = choice.upper()
if choice == 'M':
msg = input('Message Id:')
info = self.MessageInfo(msg)
elif choice == 'D':
info = self.DeleteMessage()
elif choice == 'Q': break
else : info = 'Error Action!'
return info
def DeleteMessage(self):
####删除个人消息
print()
print(' Delete Message')
MsgNo = input('Message id = ')
cur = self.conn.cursor()
sqlcmd = "delete from AllMessage where Id = %d and SenderNo = '%s'" % (MsgNo,self.TeacherNo)
info = 'Delete Success!'
if cur.execute(sqlcmd) == 0:
info = 'Delete Fail'
self.conn.rollback()
else :
self.conn.commit()
cur.close()
return info
def MainSurface(self,info):
os.system('cls')
####主界面
title = "Welcome, %s" % self.Name
body1 = '[P]Personal Information'
body2 = '[M]Message Management'
body4 = '[S]Score Mark'
body3 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body4)
print(body3)
print(info)
print('=' * self.width)
def PersonalInfoSurface(self,info):
####个人信息界面
os.system('cls')
title = 'Personal Information'
body1 = '[C]Change Information'
body2 = '[Q]Quit'
body3 = ' Name: %s' % self.Name
body4 = ' Gender: %s' % self.Gender
body5 = 'Born Date: %s' % self.Birth
body6 = ' Position: %s' % self.PositionName
body7 = ' Salary: %.2f' %self.Salary
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(info)
print('-' * self.width)
print(body3)
print(body4)
print(body5)
print(body6)
print(body7)
print('=' * self.width)
def MessageSurface(self,info):
#####消息界面
os.system('cls')
title = 'MESSAGE'
body1 = '[P]Publish Message'
body2 = '[Y]Your Message'
body3 = '[M]Message Detail'
body4 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body3)
print(body4)
print(info)
print('=' * self.width)
def PersonalMessageSurface(self,info):
#####个人消息界面
os.system('cls')
title = 'PERSONAL MESSAGE'
body1 = '[M]Message Detail'
body2 = '[D]Delete Message'
body3 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body3)
print(info)
print('=' * self.width)
if __name__ == '__main__':
conn = MySQLdb.connect(user='root',passwd = 'root',db = 'student2021')
t = Teacher(conn,'00001','123456')
t.MainFunc()
conn.close()
4、class SystemManager
def __init__(self,conn,account,pw) 构造,conn连接数据库
def MainFunc(self) 主要执行函数
def OperatTeacher(self) 操作教职工
def ScanTeacherInfo(self) 浏览教职工消息
def RegTeacher(self) 注册教职工
def ChangeTeacherInfo(self) 修改教职工信息
def InitTeacherPassword(self) 初始化教职工密码
def DeleteTeacher(self,TeacherNo) 删除教职工信息
def GetPositionInfo(self) 查询职位编号对应信息
def OperatStudent(self) 操作学生
def ScanStudentInfo(self) 浏览学生消息
def RegStudent(self) 注册学生
def ChangeStudentInfo(self,StudentNo) 修改学生信息
def InitStudentPassword(self,StudentNo) 初始化学生密码
def DeleteStudent(self,StudentNo) 删除学生信息
def OperatMessage(self) 操作消息
def MessageList(self) 查看消息列表
def MessageInfo(self,MsgNo) 消息详细信息
def CreateMessage(self) 发布消息
def DeleteMessage(self,MsgNo) 删除消息
def CheckMessage(self) 审核消息
def MessageInfo(self,MsgNo) 查看详细消息
def WaitMessageSurface(self,info) 审核消息界面
def MainSurface(self,info) 主界面
def StudentInfoSurface(self,info) 学生信息界面
def TeacherInfoSurface(self,info) 教职工信息界面
def MessageSurface(self,info) 消息列表界面
import MySQLdb
import time
import os
#import exceptions
class SystemManager:
def __init__(self,conn,account,pw):
self.conn = conn
self.width = 150
self.account = account
cur = self.conn.cursor()
self.password= pw
def MainFunc(self):
info = ''
while True:
self.MainSurface(info)
choice = input('What to do?')
choice = choice.upper()
if choice == 'T':
self.OperatTeacher()
elif choice == 'M':
self.OperatMessage()
elif choice == 'S':
self.OperatStudent()
elif choice == 'Q': break;
else: info = 'Error Action!'
def OperatTeacher(self):
####操作教职工
info = ''
while True:
self.TeacherInfoSurface(info)
self.ScanTeacherInfo()
print('-' * self.width)
choice =input('What to do?')
choice = choice.upper()
if choice == 'R':
info = self.RegTeacher()
elif choice == 'C':
info = self.ChangeTeacherInfo()
elif choice == 'I':
info = self.InitTeacherPassword()
elif choice == 'D':
info = self.DeleteTeacher()
elif choice == 'Q': break
else: info = 'Error Acction!'
def ScanTeacherInfo(self):
####浏览教职工消息
cur = self.conn.cursor()
sqlcmd = "select T.Id,T.Name,T.TeacherNo,T.Gender,T.Birth,P.PositionName,T.Salary from TeacherInfo T,PositionList P where T.PositionNo = P.PositionNo"
cur.execute(sqlcmd)
print('%3s|%20s|%12s|%8s|%15s|%15s|%15s|' % ('Id','Name','TeacherNo','Gender','BornDate','Position','Salary'))
while True:
res = cur.fetchone()
if not res: break
print('%3d|%20s|%12s|%8s|%15s|%15s|%15.2f|' % (res[0],res[1],res[2],res[3],res[4],res[5],res[6]))
print('-' * self.width)
cur.close()
def RegTeacher(self):
####注册教职工
cur = self.conn.cursor()
print()
title = ' Register New Teacher'
print(title)
name =input(' Name:')
number =input(' Teacher Number:')
gender =input(' Gender:')
birth =input(' Born Date:')
pos = self.PrintPositionInfo()
position=input('Position Number:')
salary = input(' Salary:')
sqlcmd = "insert into TeacherInfo(Name,TeacherNo,Gender,Birth,PositionNo,Salary) values('%s','%s','%s','%s',%s,%s)" % (name,number,gender,birth,position,salary)
res = cur.execute(sqlcmd)
info = 'Register Success!'
if res == 0:
info = 'Register Fail!'
self.conn.rollback()
else :
sqlcmd = 'select Password from DefaultPassword where AccountLevel = 1'
if cur.execute(sqlcmd) == 0:
info = 'Register Fail!'
self.conn.rollback()
else :
pw = cur.fetchone()
sqlcmd = "insert into LoginAccount(Account,Password,AccountLevel) values('%s','%s',1)" % (number,pw[0])
if cur.execute(sqlcmd) == 0:
info = 'Register Fail!'
self.conn.rollback()
else :
self.conn.commit()
cur.close()
return info
def ChangeTeacherInfo(self):
####修改教职工信息
cur = self.conn.cursor()
print()
title = ' Change Teacher Information'
print(title)
teacherNo =input('TeacherNo:')
sqlcmd = "select Name,TeacherNo,Gender,Birth,PositionNo,Salary from TeacherInfo where TeacherNo = '%s'" % teacherNo
res = cur.execute(sqlcmd)
info = 'Change Success!'
if res == 0:
info = 'Cannot find this teacher'
else :
temp = cur.fetchone()
print('old information: %s %s %s %s %d %.2f' % (temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]))
name =input(' Name:')
number =input(' Teacher Number:')
gender =input(' Gender:')
birth =input(' Born Date:')
self.PrintPositionInfo()
position=input('Position Number:')
salary = input(' Salary:')
sqlcmd = "update TeacherInfo Set Name='%s',TeacherNo='%s',Gender='%s',Birth='%s',PositionNo='%s',Salary='%s' where TeacherNo = '%s'" % (name,number,gender,birth,position,salary,teacherNo)
res = cur.execute(sqlcmd)
if res == 0:
info = 'Change Fail!'
self.conn.rollback()
else :
if number != temp[1]:
sqlcmd = "update LoginAccount set Account='%s' where Account='%s'" %(number,temp[1])
if cur.execute(sqlcmd) == 0:
info = 'Change Fail!'
self.conn.rollback()
else :
self.conn.commit()
else :
self.conn.commit()
cur.close()
return info
def InitTeacherPassword(self):
####初始化教职工密码
cur = self.conn.cursor()
sqlcmd = 'select Password from DefaultPassword where AccountLevel = 1'
info = 'Initial Success!'
if cur.execute(sqlcmd) == 0:
info = 'Initial Fail'
self.conn.rollback()
else:
newPw = cur.fetchone()
if not newPw:
info = 'Initial Fail'
self.conn.rollback()
else:
teacherNo =input('Teacher Number:')
sqlcmd = "select Password from LoginAccount where Account = '%s'" % teacherNo
if 0 == cur.execute(sqlcmd):
info = 'Initial Fail'
self.conn.rollback()
else :
oldPw = cur.fetchone()
if oldPw[0] != newPw[0]:
sqlcmd = "update LoginAccount set Password='%s' where Account = '%s'" %(newPw[0],teacherNo)
if cur.execute(sqlcmd) == 0:
info = 'Initial Fail'
self.conn.rollback()
else:
self.conn.commit()
cur.close()
return info
def DeleteTeacher(self):
####删除教职工信息
cur = self.conn.cursor()
print(' Delete Teacher')
teacherNo =input('Teacher Number:')
sqlcmd = "delete from TeacherInfo where TeacherNo = '%s'" % teacherNo
res = cur.execute(sqlcmd)
info = 'Delete Success!'
if res == 0:
info = 'Delete Fail!'
self.conn.rollback()
else :
sqlcmd = "delete from LoginAccount where Account = '%s'" % teacherNo
res = cur.execute(sqlcmd)
if res == 0:
info = 'Delete Fail!'
self.conn.rollback()
else : self.conn.commit()
cur.close()
return info
def PrintPositionInfo(self):
cur = self.conn.cursor()
cur.execute('select PositionNo,PositionName from PositionList')
pos = []
while True:
tp = cur.fetchone()
if not tp: break;
pos.append(tp)
print(' '*10,'-'*30)
print(' '*10 ,'POSTIONS')
print(' '*10,'-'*30)
it = pos.__iter__()
while True:
try:
temp = it.next()
print(' ' * 10, temp[0],' : ',temp[1])
except:
break;
print(' '*10,'-'*30)
cur.close()
def OperatStudent(self):
####操作学生
info = ''
while True:
self.StudentInfoSurface(info)
self.ScanStudentInfo()
print('-' * self.width)
choice =input('What to do?')
choice = choice.upper()
if choice == 'R':
info = self.RegStudent()
elif choice == 'C':
info = self.ChangeStudentInfo()
elif choice == 'I':
info = self.InitStudentPassword()
elif choice == 'D':
info = self.DeleteStudent()
elif choice == 'Q': break;
else: info = 'Error Acction!'
def ScanStudentInfo(self):
####浏览学生消息
cur = self.conn.cursor()
sqlcmd = "select S.Id,S.Name,S.StudentNo,S.Gender,S.Birth,S.Grade,S.Academy,S.Major,T.Name from StudentInfo S,TeacherInfo T where S.TeacherNo = T.TeacherNo"
cur.execute(sqlcmd)
print('%3s|%20s|%15s|%8s|%15s|%5s|%20s|%20s|%20s|' % ('Id','Name','Student Number','Gender','Born Date','Grade','Academy','Major','Teacher'))
while True:
res = cur.fetchone()
if not res: break
print('%3d|%20s|%15s|%8s|%15s|%5s|%20s|%20s|%20s|' % (res[0],res[1],res[2],res[3],res[4],res[5],res[6],res[7],res[8]))
print('-' * self.width)
cur.close()
def RegStudent(self):
####注册学生
cur = self.conn.cursor()
print( )
title = ' Register New Student'
print(title)
name =input(' Name:')
number =input('Student number:')
gender =input(' Gender:')
birth =input(' Born Date:')
grade =input(' Grade:')
academy=input(' Academy:')
major =input(' Major:')
teacher=input('Teacher Number:')
sqlcmd = "insert into StudentInfo(Name,StudentNo,Gender,Birth,Grade,Academy,Major,TeacherNo) values('%s','%s','%s','%s','%s','%s','%s','%s')" % (name,number,gender,birth,grade,academy,major,teacher)
res = cur.execute(sqlcmd)
info = 'Register Success!'
if res == 0:
info = 'Register Fail!'
self.conn.rollback()
else :
sqlcmd = 'select Password from DefaultPassword where AccountLevel = 2'
if cur.execute(sqlcmd) == 0:
info = 'Register Fail!'
self.conn.rollback()
else :
pw = cur.fetchone()
sqlcmd = "insert into LoginAccount(Account,Password,AccountLevel) values('%s','%s',2)" % (number,pw[0])
if cur.execute(sqlcmd) == 0:
info = 'Register Fail!'
self.conn.rollback()
else :
self.conn.commit()
cur.close()
return info
def ChangeStudentInfo(self,):
####修改学生信息
cur = self.conn.cursor()
print()
title = ' Change Student Information'
print(title)
studentNo =input('Student Number:')
sqlcmd = "select Name,StudentNo,Gender,Birth,Grade,Academy,Major,TeacherNo from StudentInfo where StudentNo = '%s'" % studentNo
res = cur.execute(sqlcmd)
info = 'Change Success!'
if res == 0:
info = 'Cannot find this student'
else :
temp = cur.fetchone()
print('old information: |%s| |%s| |%s| |%s| |%s| |%s| |%s| |%s|' % (temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7]))
name =input(' Name:')
number =input('Student Number:')
gender =input(' Gender:')
birth =input(' Born Date:')
grade =input(' Grade:')
academy=input(' Academy:')
major =input(' Major:')
teacher=input('Teacher Number:')
sqlcmd = "update StudentInfo Set Name='%s',StudentNo='%s',Gender='%s',Birth='%s',Grade='%s',Academy='%s',Major='%s',TeacherNo='%s' where StudentNo = '%s'" % (name,number,gender,birth,grade,academy,major,teacher,studentNo)
if cur.execute(sqlcmd) == 0:
info = 'Change Fail!'
self.conn.rollback()
else :
if number != temp[1]:
sqlcmd = "update LoginAccount set Account='%s' where Account='%s'" %(number,temp[1])
if cur.execute(sqlcmd) == 0:
info = 'Change Fail!'
self.conn.rollback()
else :
self.conn.commit()
else :
self.conn.commit()
cur.close()
return info
def InitStudentPassword(self):
####初始化学生密码
cur = self.conn.cursor()
sqlcmd = 'select Password from DefaultPassword where AccountLevel = 2'
info = 'Initial Success!'
if cur.execute(sqlcmd) == 0:
info = 'Initial Fail'
self.conn.rollback()
else:
newPw = cur.fetchone()
if not newPw:
info = 'Initial Fail'
self.conn.rollback()
else:
studentNo =input('Student Number:')
sqlcmd = "select Password from LoginAccount where Account = '%s'" % studentNo
cur.execute(sqlcmd)
oldPw = cur.fetchone()
if oldPw[0] != newPw[0]:
sqlcmd = "update LoginAccount set Password='%s' where Account = '%s'" %(newPw[0],studentNo)
if cur.execute(sqlcmd) == 0:
info = 'Initial Fail'
self.conn.rollback()
else:
self.conn.commit()
cur.close()
return info
def DeleteStudent(self,):
####删除学生信息
cur = self.conn.cursor()
print(' Delete Student')
studentNo =input('Student Number:')
sqlcmd = "delete from StudentInfo where StudentNo = '%s'" % studentNo
res = cur.execute(sqlcmd)
info = 'Delete Success!'
if res == 0:
info = 'Delete Fail!'
self.conn.rollback()
else :
sqlcmd = "delete from LoginAccount where Account = '%s'" % studentNo
res = cur.execute(sqlcmd)
if res == 0:
info = 'Delete Fail!'
self.conn.rollback()
else : self.conn.commit()
cur.close()
return info
def OperatMessage(self):
####操作消息
info = ''
while True:
self.MessageSurface(info)
self.MessageList()
choice =input('What to do?')
choice = choice.upper()
if choice == 'D':
info = self.DeleteMessage()
elif choice == 'P':
info = self.CreateMessage()
elif choice == 'C':
info = self.CheckMessage()
elif choice == 'M':
msg = input('Message Id:')
info = self.MessageInfo(msg)
elif choice == 'Q': break
else : info = 'Error Action!'
def MessageInfo(self,MsgNo):
####查看详细消息, MsgNo消息编号
cur = self.conn.cursor()
sqlcmd = "select SenderName,SendTime,Title,Content from AllMessage where Id = %d" % MsgNo
if cur.execute(sqlcmd) == 0:
cur.close()
return 'Read Fail!'
article = cur.fetchone()
cur.close()
os.system('cls')
print('=' * self.width)
print(' ' * ((self.width - len(article[2]))/2) , article[2])
head = article[0] + ' ' + str(article[1])
print(' ' * ((self.width - len(head))/2) , head)
print('-' * self.width)
print(article[3])
print('=' * self.width)
input('Press any key to return!')
return ''
def MessageList(self):
####查看消息列表
cur = self.conn.cursor()
print()
sqlcmd = "select Id,SenderName,SendTime,Title from AllMessage where statu = 'pass'"
if cur.execute(sqlcmd) == 0: return
print('-' * self.width)
while True:
temp = cur.fetchone()
if not temp: break;
print('%3s %-20s%-50s%s' % (temp[0],temp[1],temp[3],temp[2]))
print('-' * self.width)
cur.close()
def CreateMessage(self):
####发布消息
print()
print(' Publish Messsage')
title =input('Message Title:')
path =input('Message Path:')
fp = open(path,'r')
body = fp.read()
fp.close()
sqlcmd = "insert into AllMessage(MsgLevel,SenderNo,SenderName,SendTime,Title,Content,statu) values(0,'%s','Admin',now(),'%s','%s','pass')" % (self.account,title,body)
cur = self.conn.cursor()
info = 'Publish Success!'
if 0 == cur.execute(sqlcmd):
info = 'Publish Fail'
self.conn.rollback()
else:
self.conn.commit()
cur.close()
return info
def DeleteMessage(self):
####删除消息
print ()
print(' Delete Message')
MsgNo = input('Message id = ')
cur = self.conn.cursor()
sqlcmd = "delete from AllMessage where Id = %d" % MsgNo
info = 'Delete Success!'
if cur.execute(sqlcmd) == 0:
info = 'Delete Fail'
self.conn.rollback()
else :
self.conn.commit()
cur.close()
return info
def CheckMessage(self):
####审核消息
cur = self.conn.cursor()
MsgCount = cur.execute("select Id,SenderNo,SenderName,SendTime,Title,Content from AllMessage where statu = 'wait'")
info = 'All Messages Were Checked!'
MsgInfo = 'You have %d messages need to check!' % MsgCount
while MsgCount > 0:
self.WaitMessageSurface(MsgInfo)
msg = cur.fetchone()
print(' ' * ((self.width - len(msg[4]))/2),msg[4])
print('Sender Name:',msg[2], ' Sender Number:',msg[1], ' Time:',msg[3])
print(msg[5])
print('-' * self.width)
choice =input('What to do?')
choice = choice.upper()
MsgCount -= 1
MsgInfo = 'You have %d messages need to check!' % MsgCount
if choice == 'I':
continue
elif choice == 'P':
sqlcmd = "update AllMessage set statu = 'pass' where Id = %d" % msg[0]
if cur.execute(sqlcmd) == 0:
MsgInfo = 'Check Fail!'
self.conn.rollback()
else: self.conn.commit()
elif choice == 'F':
sqlcmd = "update AllMessage set statu = 'fail' where Id = %d" % msg[0]
if cur.execute(sqlcmd) == 0:
MsgInfo = 'Check Fail!'
self.conn.rollback()
else: self.conn.commit()
elif choice == 'Q': break;
else : info = 'Error Action!'
cur.close()
if MsgCount != 0:
info = 'Still have %d Messages wait for dealing!' % MsgCount
return info
def MainSurface(self,info):
#####主界面
os.system('cls')
title = 'Welcome, Administor!'
body1 = '[T]Teachers Information'
body2 = '[S]Students Information'
body3 = '[M]Message Information'
body4 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print (body3)
print(body4)
print (info)
print ('=' * self.width)
def StudentInfoSurface(self,info):
####学生信息界面
os.system('cls')
title = 'STUDENT LIST'
body1 = '[R]Register New Student'
body2 = '[C]Change Student Information'
body3 = '[I]Initial Student Password'
body4 = '[D]Delete Student Information'
body5 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body3)
print(body4)
print(body5)
print(info)
print('=' * self.width)
def TeacherInfoSurface(self,info):
####教职工信息界面
os.system('cls')
title = 'TEACHER LIST'
body1 = '[R]Register New Teacher'
body2 = '[C]Change Teacher Information'
body3 = '[I]Initial Teacher Password'
body4 = '[D]Delete Teacher Information'
body5 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body3)
print(body4)
print(body5)
print(info)
print('=' * self.width)
def MessageSurface(self,info):
####消息列表界面
os.system('cls')
title = 'MESSAGE LIST'
body1 = '[P]Publish Message'
body2 = '[C]Check Message'
body3 = '[D]Delete Message'
body4 = '[M]Message Detail'
body5 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body3)
print(body4)
print(body5)
print(info)
print('=' * self.width)
def WaitMessageSurface(self,info):
####审核消息界面
os.system('cls')
title = 'CHECK MESSAGE'
body1 = '[I]Ignore'
body2 = '[P]Pass'
body3 = '[F]Fail'
body4 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body3)
print(body4)
print(info)
print('=' * self.width)
if __name__ == '__main__':
conn = MySQLdb.connect(user = 'root',passwd = 'root',db = 'student2021');
sm = SystemManager(conn,'123','123456')
sm.MainFunc()
conn.close()
更多推荐
所有评论(0)