python+mysql实现教务管理系统
本文实例为大家分享了python实现教务管理系统,供大家参考,具体内容如下
mysql+python构成教务管理系统,提供系统管理员,教职工,学生三级。有注册,添加,修改,发布信息等功能。
Login.py
#-*-coding:utf-8-*-
#####系统登录
importos
importMySQLdb
importtime
classLogin:
def__init__(self,conn):
self.account=''
self.password=''
self.level=2
self.conn=conn
defLoginSurface(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),title
print''*((width-len(body1))/2),body1
print''*((width-len(body1))/2),body2
print''*((width-len(body1))/2),body3
print''*((width-len(body1))/2),body4
print''*((width-len(info))/2),info
print'-'*width
defMainFunc(self):
err=''
whileTrue:
self.LoginSurface(err)
level=raw_input('Access:')
level=level.upper()
iflevel=='A':self.level=0
eliflevel=='T':self.level=1
eliflevel=='S':self.level=2
eliflevel=='Q':returnFalse
else:
err='ErrorAction!'
continue
self.account=raw_input('Account:')
self.password=raw_input('Password:')
ifself.CheckAccount():
err='LoginSuccess!'
self.LoginSurface(err)
print'Pleasewait...'
time.sleep(3)
returnTrue;
else:
err='LoginFailed!'
defGetLoginAccount(self):
return[self.account,self.password,self.level]
defCheckAccount(self):
cur=self.conn.cursor()
sqlcmd="selectAccount,Password,AccountLevelfromLoginAccountwhereAccount='%s'"%self.account
ifcur.execute(sqlcmd)==0:returnFalse
temp=cur.fetchone()
cur.close()
iftemp[1]==self.passwordandtemp[2]==self.level:
returnTrue
else:returnFalse
defQuit(self):
pass
if__name__=='__main__':
conn=MySQLdb.connect(user='root',passwd='123456',db='test');
a=Login(conn)
a.MainFunc()
a.Quit()
conn.close()
main.py
#-*-coding:utf-8-*- ####系统入口 importos importMySQLdb importStudent importTeacher importLogin importSystemManager if__name__=='__main__': conn=MySQLdb.connect(user='root',passwd='123456',db='test') log=Login.Login(conn) iflog.MainFunc(): account=log.GetLoginAccount() ifaccount[2]==0: usr=SystemManager.SystemManager(conn,account[0],account[1]) usr.MainFunc() elifaccount[2]==1: usr=Teacher.Teacher(conn,account[0],account[1]) usr.MainFunc() elifaccount[2]==2: usr=Student.Student(conn,account[0],account[1]) usr.MainFunc() else: conn.close() raiseexception() conn.close()
Student.py
#-*-coding:utf-8-*-
####学生账号
importMySQLdb
importos
classStudent:
def__init__(self,conn,account,passwd):
###构造,conn连接数据库
cur=conn.cursor()
sqlcmd="selectName,Gender,Birth,Academy,Major,Grade,TeacherNofromStudentInfowhereStudentNo='%s'"%account
cur.execute(sqlcmd)
res=cur.fetchone()
sqlcmd="selectNamefromTeacherInfowhereTeacherNo='%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.Accademy=res[3]
self.Major=res[4]
self.Grade=res[5]
self.Teacher=TeacherName[0]
defMainFunc(self):
###主要执行函数
info=''
whileTrue:
self.MainSurface(info)
choice=raw_input('Whattodo?')
choice=choice.upper()
ifchoice!='P'andchoice!='M'andchoice!='Q':
info='ErrorAction!'
continue
ifchoice=='P':
info=self.PersonalInfo()
elifchoice=='M':
info=self.OperatMessage()
else:break
defPersonalInfo(self):
###个人信息
info=''
whileTrue:
self.PersonalInfoSurface(info)
choice=raw_input('Whattodo?')
choice=choice.upper()
ifchoice!='C'andchoice!='Q':
info='ErrorAction!'
continue
ifchoice=='C':
info=self.ChangePersonalInfo()
else:break
returninfo
defChangePersonalInfo(self):
###修改个人信息
NewGender=self.Gender
NewBirth=self.Birth
NewPw=self.Password
whileTrue:
choice=raw_input('ChangeGender?(y/n)')
choice=choice.lower()
ifchoice=='y':
NewGender=raw_input('NewGender:')
break
elifchoice=='n':break
else:pass
whileTrue:
choice=raw_input('changeBornDate?(y/n)')
choice=choice.lower()
ifchoice=='y':
NewBirth=raw_input('NewBornDate:')
break
elifchoice=='n':break
else:pass
whileTrue:
choice=raw_input('changePassword?(y/n)')
choice=choice.lower()
ifchoice=='y':
NewPw=raw_input('NewPassword:')
break
elifchoice=='n':break
else:pass
info='ChangeSuccess!'
cur=self.conn.cursor()
ifNewGender!=self.GenderorNewBirth!=self.Birth:
sqlcmd="updateStudentInfosetGender='%s',Birth='%s'whereStudentNo='%s'"%(NewGender,NewBirth,self.account)
ifcur.execute(sqlcmd)==0:
self.conn.rollback()
cur.close()
return'ChangeFail!'
ifNewPw!=self.Password:
sqlcmd="updateLoginAccountsetPassword='%s'whereAccount='%s'"%(NewPw,self.account)
ifcur.execute(sqlcmd)==0:
self.conn.rollback()
cur.close()
return'ChangeFail!'
else:
self.conn.commit()
self.Gender=NewGender
self.Birth=NewBirth
self.Password=NewPw
cur.close()
return'ChangeSuccess!'
defOperatMessage(self):
info=''
whileTrue:
self.MessageSurface(info)
self.MessageList()
choice=raw_input('Whattodo?')
choice=choice.upper()
ifchoice=='M':
msg=input('MessageId:')
info=self.MessageInfo(msg)
elifchoice=='Q':break;
else:info='ErrorAction!'
returninfo
defMessageList(self):
###查看消息列表
cur=self.conn.cursor()
print''
sqlcmd="selectId,SenderName,SendTime,TitlefromAllMessagewherestatu='pass'andMsgLevel=1"
ifcur.execute(sqlcmd)==0:return
print'-'*self.width
whileTrue:
temp=cur.fetchone()
ifnottemp:break;
print'%3d%-20s%-50s%s'%(temp[0],temp[1],temp[3],temp[2])
print'-'*self.width
cur.close()
defMessageInfo(self,MsgNo):
###查看详细消息,No消息编号
cur=self.conn.cursor()
sqlcmd="selectSenderName,SendTime,Title,ContentfromAllMessagewhereId=%d"%MsgNo
ifcur.execute(sqlcmd)==0:
cur.close()
return'ReadFail!'
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
printarticle[3]
print'='*self.width
raw_input('Pressanykeytoreturn!')
return''
defQuit(self):
###退出
pass
defMainSurface(self,info):
###主界面
os.system('cls')
print'='*self.width
title='Welcome%s!'%self.Name
body1='[P]PersonalInformation'
body2='[M]Message'
body3='[Q]Quit'
print''*((self.width-len(title))/2),title
print''*((self.width-len(body1))/2),body1
print''*((self.width-len(body1))/2),body2
print''*((self.width-len(body1))/2),body3
print''*((self.width-len(info))/2),info
print'='*self.width
defMessageSurface(self,info):
###消息界面
os.system('cls')
print'='*self.width
title='MESSAGES'
body1='[M]MessageDetail'
body2='[Q]Quit'
print''*((self.width-len(title))/2),title
print''*((self.width-len(body1))/2),body1
print''*((self.width-len(body1))/2),body2
print''*((self.width-len(info))/2),info
print'='*self.width
defPersonalInfoSurface(self,info):
###个人信息界面
os.system('cls')
print'='*self.width
title='PERSONALINFORMATION'
body1='[C]ChangeInformation'
body2='[Q]Quit'
print''*((self.width-len(title))/2),title
print''*((self.width-len(body1))/2),body1
print''*((self.width-len(body1))/2),body2
print''*((self.width-len(info))/2),info
print'-'*self.width
body3='Name:%s'%self.Name
body4='StudentNumber:%s'%self.account
body5='Gender:%s'%self.Gender
body6='Birth:%s'%self.Birth
body7='Accademy:%s'%self.Accademy
body8='Major:%s'%self.Major
body9='Grade:%s'%self.Grade
body10='Teacher:%s'%self.Teacher
print''*((self.width-len(body6))/2),body3
print''*((self.width-len(body6))/2),body4
print''*((self.width-len(body6))/2),body5
print''*((self.width-len(body6))/2),body6
print''*((self.width-len(body6))/2),body7
print''*((self.width-len(body6))/2),body8
print''*((self.width-len(body6))/2),body9
print''*((self.width-len(body6))/2),body10
print'='*self.width
if__name__=='__main__':
conn=MySQLdb.connect(user='root',passwd='123456',db='test')
stu=Student(conn,'0000001','123456')
stu.MainFunc()
conn.close()
完整代码请点击下载:python实现教务管理系统
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。