```python
```python
登录:
#-*- coding:utf-8 -*-
#####系统登录
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 = 'Chai0514R',db = 'student2021');
a = Login(conn)
a.MainFunc()
a.Quit()
conn.close()
学生:
#-- coding:utf-8 --
####学生账号
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'and choice != 'E'and choice != 'A':
info = 'Error Action!'
continue
if choice == 'P':
info = self.PersonalInfo()
elif choice == 'A':
info = self.Exam()
elif choice == 'E':
info = self.Evaluate()
elif choice == 'M':
info = self.OperatMessage()
elif choice == 'L':
info = self.ChooseLessonInfo()
elif choice == 'S':
info = self.stchoose()
else : break
def Exam(self):
#查看考试安排
cur = self.conn.cursor()
sql = """SELECT * FROM Exam WHERE studentno = 202004"""
try:
cur.execute(sql)
results = cur.fetchall()
for row in results:
studentno = row[0]
subjet = row[1]
date = row[2]
location = row[3]
print("id:%s\nname:%s\ndate:%s\nlocation:%s\n"%(studentno,subjet,date,location ))
except Exception as e:
print("查询出错:case%s"%e)
finally:
cur.close()
def Evaluate(self):
cur = self.conn.cursor()
sqlcmd = "select * from stchoose"
cur.execute(sqlcmd)
print('%10s|%10s|%10s|%20s|%8s|%20s|' % ('StudentNo','LesNo','LesName','TeacherNo','Date','ClassRoom'))
while True:
res = cur.fetchone()
if not res: break
print('%10s|%10s|%10s|%20s|%8s|%10s' % (res[0],res[1],res[2],res[3],res[4],res[5]))
Score =input('enter Teacher score:')
sqlcmd = "select * from stchoose where TeacherNo= '001'"
cur.execute(sqlcmd)
res = cur.fetchone()
print(res[0])
print(res[1])
print(res[2])
print(res[3])
print(res[4])
print(res[5])
sqlcmd = "insert into T_score(TeacherNo,Score) values('%s','%s')" % (res[3],Score)
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Score Successfully!'
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 :
info =stu.MainFunc()
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'
bd4='[E]Exit'
print(bd0)
print(bd1)
print(bd2)
print(bd4)
choice = input('What to do?')
choice = choice.upper()
if choice != 'G' and choice != 'B' and choice != 'P' and choice != 'Q'and choice != 'E':
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
elif choice =='E':
stu.PersonalInfo()
else :
print("输入有误,请重新输入")
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)
body1 = '[S]Select Course'
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:')
#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,TeacherNo,date,classroom) values('%s','%s','%s','%s','%s','%s')" % ("202004",res[0],res[1],res[2],res[3],res[4])
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Choose Class Successfully!'
elif choice == 'Q':
return ''
else :
info = 'Error Action!'
print('-' * self.width)
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('%20s|%10s|%20s|%25s|%20s|%20s|' % ('StudentNo','LesNo','lessonName','TeacherNo','Time','ClassRoom'))
while True:
res = cur.fetchone()
if not res:
break
print('%20s|%10s|%20s|%25s|%20s|%20s|' % (res[0],res[1],res[2],res[3],res[4],res[5]))
print('-' * self.width)
body3 = '[D]Delete Course'
body1 = '[S]Search Score'
body2 = '[Q]Quit'
print(body3)
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 == 'D':
cur = self.conn.cursor()
sqlcmd = "select * from stchoose where StudentNo=202004"
cur.execute(sqlcmd)
print('%10s|%24s|%20s|%20s|%19s|%15s|' % ('StudentNo','lesNo','LesName','TeacherNo','date','classroom'))
while True:
res = cur.fetchone()
if not res:break
print('%10s|%24s|%20s|%20s|%17s|%15s|' % (res[0],res[1],res[2],res[3],res[4],res[5]))
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 stchoose 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 = "delete from stchoose where LesNo= '%s'" % Cno
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Quick Class Successfully!'
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 Sc 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):
###退出
MainSurface(self,info)
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'
body6 = '[A]Exam arrange'
body7 = '[E]Evaluation of Teaching'
body3 = '[Q]Quit'
print(title)
print(body1)
print(body2)
print(body4)
print(body5)
print(body6)
print(body7)
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 = ‘Chai0514R’,db = ‘student2021’,charset = ‘utf8’)
#stu = Student(conn,‘0000001’,‘123456’)
stu = Student(conn,‘202004’,‘123456’)
stu.MainFunc()
conn.close()
主函数:
#-*- coding:utf-8 -*-
####学生账号
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'and choice != 'E'and choice != 'A':
info = 'Error Action!'
continue
if choice == 'P':
info = self.PersonalInfo()
elif choice == 'A':
info = self.Exam()
elif choice == 'E':
info = self.Evaluate()
elif choice == 'M':
info = self.OperatMessage()
elif choice == 'L':
info = self.ChooseLessonInfo()
elif choice == 'S':
info = self.stchoose()
else : break
def Exam(self):
#查看考试安排
cur = self.conn.cursor()
sql = """SELECT * FROM Exam WHERE studentno = 202004"""
try:
cur.execute(sql)
results = cur.fetchall()
for row in results:
studentno = row[0]
subjet = row[1]
date = row[2]
location = row[3]
print("id:%s\nname:%s\ndate:%s\nlocation:%s\n"%(studentno,subjet,date,location ))
except Exception as e:
print("查询出错:case%s"%e)
finally:
cur.close()
def Evaluate(self):
cur = self.conn.cursor()
sqlcmd = "select * from stchoose"
cur.execute(sqlcmd)
print('%10s|%10s|%10s|%20s|%8s|%20s|' % ('StudentNo','LesNo','LesName','TeacherNo','Date','ClassRoom'))
while True:
res = cur.fetchone()
if not res: break
print('%10s|%10s|%10s|%20s|%8s|%10s' % (res[0],res[1],res[2],res[3],res[4],res[5]))
Score =input('enter Teacher score:')
sqlcmd = "select * from stchoose where TeacherNo= '001'"
cur.execute(sqlcmd)
res = cur.fetchone()
print(res[0])
print(res[1])
print(res[2])
print(res[3])
print(res[4])
print(res[5])
sqlcmd = "insert into T_score(TeacherNo,Score) values('%s','%s')" % (res[3],Score)
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Score Successfully!'
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 :
info =stu.MainFunc()
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'
bd4='[E]Exit'
print(bd0)
print(bd1)
print(bd2)
print(bd4)
choice = input('What to do?')
choice = choice.upper()
if choice != 'G' and choice != 'B' and choice != 'P' and choice != 'Q'and choice != 'E':
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
elif choice =='E':
stu.PersonalInfo()
else :
print("输入有误,请重新输入")
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)
body1 = '[S]Select Course'
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:')
#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,TeacherNo,date,classroom) values('%s','%s','%s','%s','%s','%s')" % ("202004",res[0],res[1],res[2],res[3],res[4])
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Choose Class Successfully!'
elif choice == 'Q':
return ''
else :
info = 'Error Action!'
print('-' * self.width)
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('%20s|%10s|%20s|%25s|%20s|%20s|' % ('StudentNo','LesNo','lessonName','TeacherNo','Time','ClassRoom'))
while True:
res = cur.fetchone()
if not res:
break
print('%20s|%10s|%20s|%25s|%20s|%20s|' % (res[0],res[1],res[2],res[3],res[4],res[5]))
print('-' * self.width)
body3 = '[D]Delete Course'
body1 = '[S]Search Score'
body2 = '[Q]Quit'
print(body3)
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 == 'D':
cur = self.conn.cursor()
sqlcmd = "select * from stchoose where StudentNo=202004"
cur.execute(sqlcmd)
print('%10s|%24s|%20s|%20s|%19s|%15s|' % ('StudentNo','lesNo','LesName','TeacherNo','date','classroom'))
while True:
res = cur.fetchone()
if not res:break
print('%10s|%24s|%20s|%20s|%17s|%15s|' % (res[0],res[1],res[2],res[3],res[4],res[5]))
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 stchoose 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 = "delete from stchoose where LesNo= '%s'" % Cno
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Quick Class Successfully!'
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 Sc 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):
###退出
MainSurface(self,info)
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'
body6 = '[A]Exam arrange'
body7 = '[E]Evaluation of Teaching'
body3 = '[Q]Quit'
print(title)
print(body1)
print(body2)
print(body4)
print(body5)
print(body6)
print(body7)
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 = 'Chai0514R',db = 'student2021',charset = 'utf8')
#stu = Student(conn,'0000001','123456')
stu = Student(conn,'202004','123456')
stu.MainFunc()
conn.close()
管理员:
#-- coding: utf-8 --
####管理员账户
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|%21s|%12s|%10s|%15s|%15s|%15s|' % ('Id','Name','TeacherNo','Gender','BornDate','Position','Salary',))
while True:
res = cur.fetchone()
if not res:
break
print('%3d|%20s|%12s|%9s|%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 == 'A':
info = self.Arrange()
elif choice == 'Q': break;
else: info = 'Error Acction!'
def Arrange(self):
#考试安排
cur = self.conn.cursor()
print('Lessons :')
sqlcmd = "select s.StudentNo,s.LesNo,s.LesName,t.Name,s.date,s.ClassRoom from stchoose s,teacherinfo T where s.TeacherNo = T.TeacherNo "
cur.execute(sqlcmd)
print('%20s|%10s|%20s|%18s|%25s|%20s|' % ('StudentNo','LesNo','lessonName','TeacherName','Time','ClassRoom'))
while True:
res = cur.fetchone()
if not res:
break
print('%20s|%10s|%20s|%17s|%25s|%20s|' % (res[0],res[1],res[2],res[3],res[4],res[5]))
print('-' * self.width)
studentNo =input('Student Number:')
info = 'Arrange Success!'
if res == 0:
info = 'Cannot find this student'
else :
temp = cur.fetchone()
number =input('Student Number:')
subjet =input(' subjet:')
date =input(' date:')
location=input(' location:')
sqlcmd = "insert into exam(studentno,subjet,date,location) values('%s','%s','%s','%s')" % (number,subjet,date,location)
if cur.execute(sqlcmd) == 0:
return('error')
else:
self.conn.commit()
cur.close()
return 'Arrange Successfully!'
def ScanStudentInfo(self):
####浏览学生消息
cur = self.conn.cursor()
sqlcmd = "select S.Id, S.Name, S.Gender, S.StudentNo, S.Birth, S.Academy, T.Name , S.Major, S.Grade from studentinfo S,teacherinfo T where S.TeacherNo = T.TeacherNo"
cur.execute(sqlcmd)
print('%3s|%13s|%15s|%16s|%15s|%21s|%17s|%20s|%20s|' % ('Id','Name','Gender','Student Number','Born Date','Academy','Teacher','Major','Grade'))
while True:
res = cur.fetchone()
if not res:
break
print('%3d|%11s|%14s|%16s|%15s|%12s|%16s|%17s|%18s|'%(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 = 2"
if cur.execute(sqlcmd) == 0:
cur.close()
return 'Read Fail!'
article = cur.fetchone()
cur.close()
os.system('cls')
print("SenderName:\n")%temp[0]
print("SendTime:%SenderName\n")%temp[1]
print("Title:\n")%temp[2]
print("Content:\n")%temp[3]
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')
MsgLevel =input('Message MsgLevel:')
SenderNo =input('Message SenderNo:')
SenderName =input('Message SenderName:')
SendTime =input('Message SendTime:')
Title =input('Message Title:')
Content =input('Message Content:')
statu =input('Message statu:')
sqlcmd = "insert into AllMessage(MsgLevel,SenderNo,SenderName,SendTime,Title,Content,statu) values('%s','%s','%s','%s','%s','%s','%s')" % (MsgLevel,SenderNo,SenderName,SendTime,Title,Content,statu)
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 = 1"
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'
body6 = '[A]Arrange Exam'
body5 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body3)
print(body4)
print(body6)
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 = ‘Chai0514R’,db = ‘student2021’,charset=‘utf8’);
sm = SystemManager(conn,‘2021406’,‘123456’)
sm.MainFunc()
conn.close()
教师:
#-- coding:utf-8 --
####教师帐号
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 == 'C':
info = self.Score()
elif choice == 'Q': break
else : info = 'Error Action'
def Score(self):
#查看学生评分
cur = self.conn.cursor()
sql = """SELECT * FROM T_score """
try:
cur.execute(sql)
results = cur.fetchall()
for row in results:
TeacherNo = row[0]
Score = row[1]
print("TeacherNo:%s\nScore:%s\n"%(TeacherNo,Score ))
except Exception as e:
print("查询出错:case%s"%e)
finally:
cur.close()
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 sc 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')
MsgLevel =input('Message MsgLevel:')
SenderNo =input('Message SenderNo:')
SenderName =input('Message SenderName:')
SendTime =input('Message SendTime:')
Title =input('Message Title:')
Content =input('Message Content:')
statu =input('Message statu:')
sqlcmd = "insert into AllMessage(MsgLevel,SenderNo,SenderName,SendTime,Title,Content,statu) values('%s','%s','%s','%s','%s','%s','%s')" % (MsgLevel,SenderNo,SenderName,SendTime,Title,Content,statu)
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'
body5 = '[C]Check Score'
body3 = '[Q]Quit'
print('=' * self.width)
print(title)
print(body1)
print(body2)
print(body4)
print(body5)
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 = ‘Chai0514R’,db = ‘student2021’,charset=‘utf8’)
t = Teacher(conn,‘004’,‘123456’)
t.MainFunc()
conn.close()