python-sqlite3

cpp 复制代码
import calendar

from sqlalchemy import create_engine, Column, Integer, String, Float, ForeignKey,Date
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.orm import declarative_base, sessionmaker
import datetime


engine = create_engine('sqlite:///data//data.db', echo=True)# 创建数据库引擎,使用 SQLite 数据库
Base = declarative_base()# 创建基础类


class Student(Base):# 定义学生表的模型
    __tablename__ = 'Students'
    student_id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String, nullable=False)
    birth_date = Column(Date, nullable=False)
    major = Column(String, nullable=False)
    enrollment_year = Column(Integer, nullable=False)
    StudentKey1 = relationship("Enrollment", back_populates="EnrollmentKey2")

# 定义教师表的模型
class Instructor(Base):
    __tablename__ = 'Instructors'
    instructor_id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    department = Column(String, nullable=False)
    email = Column(String, nullable=False, unique=True)
    # 定义与课程的关系
    # 定义与课程的关系
    InstructorKey1 = relationship("Course", back_populates="CourseKey1")
    # courses = relationship("Enrollment", back_populates="teacher2")
    def __repr__(self):
        return f"Instructor(id={self.instructor_id}, name='{self.name}', department='{self.department}', email='{self.email}')"


# # 定义课程表模型
class Course(Base):
    __tablename__ = 'Courses'
    course_id = Column(Integer, primary_key=True, autoincrement=True)
    course_name = Column(String, nullable=False)
    instructor_id = Column(Integer, ForeignKey('Instructors.instructor_id', ondelete='CASCADE', onupdate='CASCADE'),  nullable=False)
    credits = Column(Float, nullable=False)
    # 定义与教师的关系
    CourseKey1 = relationship("Instructor", back_populates="InstructorKey1")
    CourseKey2 = relationship("Enrollment", back_populates="EnrollmentKey1")


# 定义选课表的模型
class Enrollment(Base):
    __tablename__ = 'Enrollments'
    enrollment_id = Column(Integer, primary_key=True, autoincrement=True)
    student_id = Column(Integer, ForeignKey('Students.student_id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
    course_id = Column(Integer, ForeignKey('Courses.course_id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
    semester = Column(String, nullable=False)
    grade = Column(Float, nullable=True)
    # 定义与学生和课程的关系
    EnrollmentKey1 = relationship("Course", back_populates="CourseKey2")
    EnrollmentKey2 = relationship("Student", back_populates="StudentKey1")

    def __repr__(self):
        return f"Enrollment(id={self.enrollment_id}, student_id={self.student_id}, course_id={self.course_id}, semester='{self.semester}', grade={self.grade})"



Base.metadata.create_all(engine)# 创建所有表
Session = sessionmaker(bind=engine)# 创建一个会话
session = Session()


def getInputNumber(begin, end,m):
    while True:
        try:
            user_input = float(input(f"请选择f{m}({begin}-{end}):"))
            if begin <= user_input <= end:
                return user_input
            else:
                print(f"输入无效。请确保输入的数字在 {begin} 到 {end} 的范围内。")
        except ValueError:
            print("输入无效。请确保输入的是一个数字。")



def getInputString(m):
    print(f"请输入f{m}")
    p=input()
    return p






def menu():
    print("1.增加学生信息")
    print("2.删除学生信息")
    print("3.查询学生信息")
    print("4.修改学生信息")
    print("5.增加教师信息")
    print("6.删除教师信息")
    print("7.查询教师信息")
    print("8.修改教师信息")
    print("9.增加课程信息")
    print("10.删除课程信息")
    print("11.查询课程信息")
    print("12.修改课程信息")
    print("13.教授打分")
    print("13.生成班级成绩报告")



def get_days_in_month(year, month):
    # 获取该月的天数
    _, days_in_month = calendar.monthrange(year, month)
    return days_in_month
def addStudent():
    # name=getInputString("学生名称")
    # year=getInputNumber(1960,2005,"出生年份")
    # month=getInputNumber(1,12,"出生月份")
    # day=getInputNumber(1,12,"出生月份")

    # 示例:添加一个新学生
    new_student = Student(
        name='Alice',
        birth_date=datetime.date(2000, 1, 1),  # YYYY, MM, DD
        major='Computer Science',
        enrollment_year=2018
    )
    # 将新学生添加到会话并提交
    session.add(new_student)
    session.commit()




if __name__ == '__main__':
    while True:
        s=getInputNumber(1,12,"菜单")
        if s==1:
            addStudent()
        elif s==2:
            pass
相关推荐
white-persist22 分钟前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
Java 码农1 小时前
Centos7 maven 安装
java·python·centos·maven
倔强青铜三1 小时前
苦练Python第63天:零基础玩转TOML配置读写,tomllib模块实战
人工智能·python·面试
浔川python社2 小时前
《网络爬虫技术规范与应用指南系列》(xc—3):合规实操与场景落地
python
B站计算机毕业设计之家2 小时前
智慧交通项目:Python+YOLOv8 实时交通标志系统 深度学习实战(TT100K+PySide6 源码+文档)✅
人工智能·python·深度学习·yolo·计算机视觉·智慧交通·交通标志
IT森林里的程序猿2 小时前
基于机器学习方法的网球比赛胜负趋势预测
python·机器学习·django
正牌强哥2 小时前
Futures_ML——机器学习在期货量化交易中的应用与实践
人工智能·python·机器学习·ai·交易·akshare
倔强青铜三2 小时前
苦练Python第62天:零基础玩转CSV文件读写,csv模块实战
人工智能·python·面试
郝学胜-神的一滴2 小时前
Effective Python 第43条:自定义容器类型为什么应该从 `collections.abc` 继承?
开发语言·python
银行数字化转型导师坚鹏3 小时前
如何设计优秀的企业微信私域运营实战培训方案
大数据·python·企业微信