-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
36 lines (29 loc) · 862 Bytes
/
Copy pathdatabase.py
File metadata and controls
36 lines (29 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, Float, Boolean
from sqlalchemy import insert, select
from sqlalchemy.orm import sessionmaker, Session
from pydantic import BaseModel
# ---------------------------
# Database setup (NO ORM)
# ---------------------------
DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
metadata = MetaData()
items = Table(
"items",
metadata,
Column("id", String, primary_key=True),
Column("name", String),
Column("price", Float),
)
metadata.create_all(engine)
# DB dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()