欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python+mysql实现教务管理系统

程序员文章站 2022-03-18 11:57:50
本文实例为大家分享了python实现教务管理系统,供大家参考,具体内容如下 mysql+python构成教务管理系统,提供系统管理员,教职工,学生三级。有注册,添加,修改...

本文实例为大家分享了python实现教务管理系统,供大家参考,具体内容如下

mysql+python构成教务管理系统,提供系统管理员,教职工,学生三级。有注册,添加,修改,发布信息等功能。

login.py

#-*- 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), 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
 
 def mainfunc(self):
 err = ''
 while true:
 self.loginsurface(err)
 level = raw_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 = 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.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 = 'test');
 a = login(conn)
 a.mainfunc()
 a.quit()
 conn.close()

main.py

#-*- coding:utf-8 -*-
####系统入口

import os
import mysqldb
import student
import teacher
import login
import systemmanager

if __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 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()
 sqlcmd = "select name from teacherinfo 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.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!'
 continue
 if choice == 'p':
 info = self.personalinfo()
 elif choice == 'm':
 info = self.operatmessage()
 else : break
 
 def 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!'
 continue
 if choice == 'c':
 info = self.changepersonalinfo()
 else : break
 return info
 
 def changepersonalinfo(self):
 ###修改个人信息
 newgender = self.gender
 newbirth = self.birth
 newpw = self.password
 while true:
 choice = raw_input('change gender?(y/n)')
 choice = choice.lower()
 if choice == 'y':
 newgender = raw_input('new gender:')
 break
 elif choice == 'n': break
 else : pass
 while true:
 choice = raw_input('change born date?(y/n)')
 choice = choice.lower()
 if choice == 'y':
 newbirth = raw_input('new born date:')
 break
 elif choice == 'n': break
 else : pass
 while true:
 choice = raw_input('change password?(y/n)')
 choice = choice.lower()
 if choice == 'y':
 newpw = raw_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 = 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 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 = %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
 raw_input('press any key to return!')
 return ''
 
 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'
 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
 
 def messagesurface(self,info):
 ###消息界面
 os.system('cls')
 print '=' * self.width
 title = 'messages'
 body1 = '[m]message detail'
 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
 
 def personalinfosurface(self,info):
 ###个人信息界面
 os.system('cls')
 print '=' * self.width
 title = 'personal information'
 body1 = '[c]change information'
 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 = 'student number: %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()

完整代码请点击下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。