| # database.py | |
| from sqlalchemy import create_engine, Column, Integer, String | |
| from sqlalchemy.orm import declarative_base, sessionmaker | |
| Base = declarative_base() | |
| class User(Base): | |
| __tablename__ = "users" | |
| id = Column(Integer, primary_key=True) | |
| username = Column(String(50), unique=True) | |
| hashed_password = Column(String(100)) | |
| # Initialize once in app.py | |
| engine = create_engine("sqlite:///database.db") | |
| Base.metadata.create_all(engine) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| # Dependency | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() |