zoukankan      html  css  js  c++  java
  • mysql开发简单的学员管理系统

    主题:学员管理系统

    需求:

    • 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下
    • 讲师视图
      1.   管理班级,可创建班级,根据学员qq号把学员加入班级
      2.   可创建指定班级的上课纪录,注意一节上课纪录对应多条学员的上课纪录, 即每节课都有整班学员上, 为了纪录每位学员的学习成绩,需在创建每节上课纪录是,同时 为这个班的每位学员创建一条上课纪录
      3.   为学员批改成绩, 一条一条的手动修改成绩
    • 学员视图
    1. 提交作业
    2. 查看作业成绩
    3. 一个学员可以同时属于多个班级,就像报了Linux的同时也可以报名Python一样, 所以提交作业时需先选择班级,再选择具体上课的节数
    4. 附加:学员可以查看自己的班级成绩排名

      

    思路:要想解决这个问题,我们就要理清有多少个对象,对象之间的关系 ,其实我觉得我们用的操作就是用到了Mysql的sqlalchemy的多对多的思想模块

    对象:1.班级 : 名字   学员    2.学员: 姓名  qq号 (qq号就相当于某学员的身份证,我们通过筛选qq好来操作学员信息)  成绩(提交作业才会有成绩) 

               3.讲师 : 名字  班级(每个老师对应着多个班级)

               4.课节表: 名字           5.上课记录表: 用来记录信息产生的关联

    from sqlalchemy import Table, Column, Integer,String,DATE, ForeignKey
    from sqlalchemy.orm import relationship
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    import random
    engine = create_engine("mysql+pymysql://root:123456@localhost/test",
                                        encoding='utf-8')
    Base = declarative_base()
    #以这种方式创建的我们不必再赋值其他参数
    #班级与学生的多对多关系
    teacher_m2m_class = Table("teacher_m2m_class",Base.metadata,
                              Column("teacher_id", Integer, ForeignKey("teacher.teacher_id")),
                              Column("class_id", Integer, ForeignKey("class.class_id")),
                              )
    # 班级与学生的对应关系表
    class_m2m_student = Table("class_m2m_student",Base.metadata,
                              Column("class_id",Integer,ForeignKey("class.class_id")),
                              Column("stu_id", Integer, ForeignKey("student.stu_id")),
                              )
    class Class_m2m_Lesson(Base):
        '''班级和课节对应表'''
        __tablename__ = "class_m2m_lesson"
        id =  Column(Integer, primary_key=True)
        class_id = Column(Integer,ForeignKey("class.class_id"))
        lesson_id = Column(Integer, ForeignKey("lesson.lesson_id"))
        classes = relationship("Class",backref="class_m2m_lessons")
        lessons = relationship("Lesson", backref="class_m2m_lessons")
        def __repr__(self):
            return "%s %s" % (self.classes,self.lessons)
    class Study_record(Base):
        "上课记录"
        __tablename__ = "study_record"
        id = Column(Integer,primary_key=True)
        class_m2m_lesson_id = Column(Integer,ForeignKey("class_m2m_lesson.id"))
        stu_id = Column(Integer, ForeignKey("student.stu_id"))
        status = Column(String(32),nullable=False)
        score = Column(Integer,nullable=True)
        class_m2m_lessons = relationship("Class_m2m_Lesson",backref="my_study_record")
        students = relationship("Student", backref="my_study_record")
        def __repr__(self):
           return  "33[35;0m%s,%s,状态:【%s】,成绩:【%s】33[0m"%(self.class_m2m_lessons,self.students,self.status,self.score)
    class Teacher(Base):
        "讲师"
        __tablename__ = "teacher"
        teacher_id = Column(Integer, primary_key=True)
        teacher_name = Column(String(32), nullable=False, unique=True)   #唯一
        classes = relationship("Class", secondary=teacher_m2m_class, backref="teachers")
        def __repr__(self):
            return "讲师:【%s】"%self.teacher_name
    class Class(Base):
        "班级"
        __tablename__ ="class"
        class_id = Column(Integer, primary_key=True)
        class_name = Column(String(32), nullable=False,unique=True)
        course =  Column(String(32), nullable=False)
        students = relationship("Student",secondary=class_m2m_student,backref="classes")
        def __repr__(self):
            return "班级名:【%s】"%self.class_name
    class Student(Base):
        "学生"
        __tablename__ ="student"
        stu_id = Column(Integer, primary_key=True)
        stu_name = Column(String(32), nullable=False, unique=True)
        QQ =  Column(Integer(), nullable=False)
        def __repr__(self):
            return "学生名:【%s】"%self.stu_name
    class Lesson(Base):
        "课节"
        __tablename__ = "lesson"
        lesson_id = Column(Integer, primary_key=True)
        lesson_name = Column(String(32), nullable=False, unique=True)
        def __repr__(self):
            return "节次名:【%s】"%self.lesson_name
    Base.metadata.create_all(engine)
    
    #初始化阶段,现在我们将列表中放入数据
    S1=Student(stu_name="mark",QQ=1)
    S2=Student(stu_name="judy",QQ=2)
    S3=Student(stu_name="jack",QQ=3)
    
    T1=Teacher(teacher_name="a")
    T2=Teacher(teacher_name="b")
    
    c1=Class(class_name="1c",course="math")
    c2=Class(class_name="2c",course="english")
    
    L1=Lesson(lesson_id=1,lesson_name="math")
    L2=Lesson(lesson_id=2,lesson_name="english")
    #实现了每个班级有多个学生,每个学生也可以报多个班级
    c1.students=[S1,S2]
    c2.students=[S1,S3]
    #实现教师班级的多对多
    T1.classes=[c2]
    T2.classes=[c1,c2]
    #初始化lessens_to_classes
    cl1= Class_m2m_Lesson(class_id=1,lesson_id=1)
    Session_class = sessionmaker(bind=engine) #创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例
    session = Session_class() #生成session实例
    session.add_all([S1,S2,S3,T1,T2,c1,c2,L1,L2,cl1])
    session.commit()
    
    class  Student_Center(object):
        "学生视图"
        def __init__(self,session):
            self.session = session
            self.authentication()
            self.handler()
        def handler(self):
            while True:
                print("33[36;1m欢迎【%s】进入学员管理系统
    "
                      "up_homework 上传作业
    "
                      "show_homework 查看作业成绩
    "
                      "show_rank 查看班级排名"
                      "exit 退出管理系统
    33[0m" % self.student_obj.stu_name)
                user_func = input("33[34;0m请输入进行操作的命令:33[0m")
                if hasattr(self, user_func):
                    getattr(self, user_func)()
        def authentication(self):
            '''认证'''
            while True:
                student_name = input("33[34;0m请输入学生名:33[0m").strip()
                self.student_obj = self.session.query(Student).filter_by(stu_name=student_name).first()
                if not self.student_obj:
                    print("33[31;1m输入错误:请输入有效的学生名33[0m")
                    continue
                else:
                    # print(self.teacher_obj)
                    break
        def up_homework(self):
            "上传作业"
            class_name = input("33[34;0m请输入班级名:33[0m")
            for class_obj in self.student_obj.classes:
                print(class_obj.class_name)
                if class_name == class_obj.class_name:
                    lesson_name = input("33[34;0m请输入的课节名(lesson):33[0m")
                    lesson_obj = self.session.query(Lesson).filter_by(lesson_name=lesson_name).first()
                    if lesson_obj:  # 输入的lesson名字存在
                        class_m2m_lesson_obj = self.session.query(Class_m2m_Lesson).filter(
                            Class_m2m_Lesson.class_id == class_obj.class_id). 
                            filter(Class_m2m_Lesson.lesson_id == lesson_obj.lesson_id).first()
                        if class_m2m_lesson_obj:  # 班级对应的课lesson表数据存在
                            study_record_obj = self.session.query(Study_record).filter(
                                Study_record.class_m2m_lesson_id == class_m2m_lesson_obj.id).filter(
                                Study_record.stu_id == self.student_obj.stu_id).first()
                            if study_record_obj:  # 上课记录存在
                                score = random.randint(10,100)
                                study_record_obj.score = score
                                self.session.commit()
                                print("上传成功")
                            else:
                                print("33[31;1m系统错误:当前上课记录已经创建33[0m")
                    else:
                        print("33[31;1m系统错误:lesson未创建33[0m")

    以上是部分代码,学生系统开发类似!

  • 相关阅读:
    测测你是男是女
    密集恐惧症候群测试图
    弱智的我
    你还单纯么
    压力测试
    理性人与感性人
    [家里蹲大学数学杂志]第248期东北师范大学2013年数学分析考研试题
    [家里蹲大学数学杂志]第254期第五届[2013年]全国大学生数学竞赛[数学类]试题
    PostgreSQL中,如何查表属于哪个数据库
    对PostgreSQL中tablespace 与 database, table的理解
  • 原文地址:https://www.cnblogs.com/shidi/p/7542634.html
Copyright © 2011-2022 走看看