python+mysql实现教务管理系统本⽂实例为⼤家分享了python实现教务管理系统,供⼤家参考,具体内容如下mysql+python构成教务管理系统,提供系统管理员,教职⼯,学⽣三级。
有注册,添加,修改,发布信息等功能。
Login.py#-*- coding:utf-8 -*-#####系统登录import osimport MySQLdbimport timeclass Login:def __init__(self,conn):self.account = ''self.password = ''self.level = 2self.conn = conndef LoginSurface(self,info):os.system('cls')width = 50title = 'LOGIN'body1 = '[A]Admin'body2 = '[T]Teacher'body3 = '[S]Student'body4 = '[Q]Quit'print '=' * widthprint ' ' * ((width-len(title))/2), titleprint ' ' * ((width-len(body1))/2),body1print ' ' * ((width-len(body1))/2),body2print ' ' * ((width-len(body1))/2),body3print ' ' * ((width-len(body1))/2),body4print ' ' * ((width-len(info))/2), infoprint '-' * widthdef MainFunc(self):err = ''while True:self.LoginSurface(err)level = raw_input('Access:')level = level.upper()if level == 'A':self.level = 0elif level == 'T': self.level = 1elif level == 'S': self.level = 2elif level =='Q': return Falseelse :err = 'Error Action!'continueself.account = raw_input('Account:')self.password = raw_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.accountif cur.execute(sqlcmd) == 0: return Falsetemp = cur.fetchone()cur.close()if temp[1] == self.password and temp[2] == self.level:return Trueelse: return Falsedef Quit(self):passif __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 -*-####系统⼊⼝import osimport MySQLdbimport Studentimport Teacherimport Loginimport SystemManagerif __name__ == '__main__':conn = MySQLdb.connect(user='root',passwd = '123456',db = 'test')log = Login.Login(conn)if log.MainFunc():account = log.GetLoginAccount()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()Student.py#-*- coding:utf-8 -*-####学⽣账号import MySQLdbimport osclass 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()sqlcmd = "select Name from TeacherInfo where TeacherNo = '%s'" % res[6]cur.execute(sqlcmd)TeacherName = cur.fetchone()cur.close()self.width = 150self.conn = connself.account = accountself.Password= passwd = 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]def MainFunc(self):###主要执⾏函数info = ''while True:self.MainSurface(info)choice = raw_input('What to do?')choice = choice.upper()if choice != 'P' and choice != 'M' and choice != 'Q':info = 'Error Action!'continueif choice == 'P':info = self.PersonalInfo()elif choice == 'M':info = self.OperatMessage()else : breakdef PersonalInfo(self):###个⼈信息info = ''while True:self.PersonalInfoSurface(info)choice = raw_input('What to do?')choice = choice.upper()if choice != 'C' and choice != 'Q':info = 'Error Action!'continueif choice == 'C':info = self.ChangePersonalInfo()else : breakreturn infodef ChangePersonalInfo(self):###修改个⼈信息NewGender = self.GenderNewBirth = self.BirthNewPw = self.Passwordwhile True:choice = raw_input('Change Gender?(y/n)')choice = choice.lower()if choice == 'y':NewGender = raw_input('New Gender:')breakelif choice == 'n': breakelse : passwhile True:choice = raw_input('change Born Date?(y/n)')choice = choice.lower()if choice == 'y':NewBirth = raw_input('New Born Date:')breakelif choice == 'n': breakelse : passwhile True:choice = raw_input('change Password?(y/n)')choice = choice.lower()if choice == 'y':NewPw = raw_input('New Password:')breakelif choice == 'n': breakelse : passinfo = '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 :mit()self.Gender = NewGenderself.Birth = NewBirthself.Password = NewPwcur.close()return 'Change Success!'def OperatMessage(self):info = ''while True:self.MessageSurface(info)self.MessageList()choice = raw_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 infodef 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: returnprint '-' * self.widthwhile True:temp = cur.fetchone()if not temp: break;print '%3d%-20s%-50s%s' % (temp[0],temp[1],temp[3],temp[2])print '-' * self.widthcur.close()def MessageInfo(self,MsgNo):###查看详细消息, No消息编号cur = self.conn.cursor()sqlcmd = "select SenderName,SendTime,Title,Content from AllMessage where Id = %d" % MsgNoif cur.execute(sqlcmd) == 0:cur.close()return 'Read Fail!'article = cur.fetchone()cur.close()os.system('cls')print '=' * self.widthprint ' ' * ((self.width - len(article[2]))/2) , article[2]head = article[0] + ' ' + str(article[1])print ' ' * ((self.width - len(head))/2) , headprint '-' * self.widthprint article[3]print '=' * self.widthraw_input('Press any key to return!')return ''def Quit(self):###退出passdef MainSurface(self,info):###主界⾯os.system('cls')print '=' * self.widthtitle = 'Welcome %s!' % body1 = '[P]Personal Information'body2 = '[M]Message'body3 = '[Q]Quit'print ' ' * ((self.width - len(title))/2),titleprint ' ' * ((self.width - len(body1))/2),body1print ' ' * ((self.width - len(body1))/2),body2print ' ' * ((self.width - len(body1))/2),body3print ' ' * ((self.width - len(info))/2),infoprint '=' * self.widthdef MessageSurface(self,info):###消息界⾯os.system('cls')print '=' * self.widthtitle = 'MESSAGES'body1 = '[M]Message Detail'body2 = '[Q]Quit'print ' ' * ((self.width - len(title))/2),titleprint ' ' * ((self.width - len(body1))/2),body1print ' ' * ((self.width - len(body1))/2),body2print ' ' * ((self.width - len(info))/2),infoprint '=' * self.widthdef PersonalInfoSurface(self,info):###个⼈信息界⾯os.system('cls')print '=' * self.widthtitle = 'PERSONAL INFORMATION'body1 = '[C]Change Information'body2 = '[Q]Quit'print ' ' * ((self.width - len(title))/2),titleprint ' ' * ((self.width - len(body1))/2),body1print ' ' * ((self.width - len(body1))/2),body2print ' ' * ((self.width - len(info))/2),infoprint '-' * self.widthbody3 = ' Name: %s' % body4 = 'Student Number: %s' % self.accountbody5 = ' Gender: %s' % self.Genderbody6 = ' Birth: %s' % self.Birthbody7 = ' Accademy: %s' % self.Accademybody8 = ' Major: %s' % self.Majorbody9 = ' Grade: %s' % self.Gradebody10= ' Teacher: %s' % self.Teacherprint ' ' * ((self.width - len(body6))/2),body3print ' ' * ((self.width - len(body6))/2),body4print ' ' * ((self.width - len(body6))/2),body5print ' ' * ((self.width - len(body6))/2),body6print ' ' * ((self.width - len(body6))/2),body7print ' ' * ((self.width - len(body6))/2),body8print ' ' * ((self.width - len(body6))/2),body9print ' ' * ((self.width - len(body6))/2),body10print '=' * self.widthif __name__ == '__main__':conn = MySQLdb.connect(user='root',passwd = '123456',db = 'test')stu = Student(conn,'0000001','123456')stu.MainFunc()conn.close()完整代码请点击下载:以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。