CREATE TABLE Materials ( id INT AUTO_INCREMENT PRIMARY KEY, type ENUM('course', 'paper', 'note') NOT NULL, title VARCHAR(255) NOT NULL, author VARCHAR(255), year YEAR, content TEXT );
This table will hold all the necessary details.
from sqlalchemy import create_engine, Column, Integer, String, Enum, Text, Date from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker Base = declarative_base() class Material(Base): __tablename__ = 'Materials' id = Column(Integer, primary_key=True) type = Column(Enum('course', 'paper', 'note')) title = Column(String(255)) author = Column(String(255)) year = Column(Date) content = Column(Text)
Next, set up the engine and session:
engine = create_engine('mysql+pymysql://username:password@localhost/dbname') Session = sessionmaker(bind=engine) session = Session()
new_material = Material(type='course', title='Advanced Algorithms', author='John Doe', year='2023', content='...') session.add(new_material) session.commit()
And to fetch them:
materials = session.query(Material).all() for m in materials: print(m.title, m.author)