PIbd-32_Kashin_M.I_API_Cour.../database.py

27 lines
813 B
Python

from typing import Optional
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
engine = create_async_engine("sqlite+aiosqlite:///questions.db")
new_session = async_sessionmaker(engine, expire_on_commit=False)
class Model(DeclarativeBase):
pass
class QuestionOrm(Model):
__tablename__ = "questions"
id: Mapped[int] = mapped_column(primary_key=True)
id_user: Mapped[int]
type_question: Mapped[bool]
question: Mapped[str]
answer: Mapped[Optional[str]]
async def create_tables():
async with engine.begin() as conn:
await conn.run_sync(Model.metadata.create_all)
async def delete_tables():
async with engine.begin() as conn:
await conn.run_sync(Model.metadata.drop_all)