You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
633 B
29 lines
633 B
|
2 weeks ago
|
"""
|
||
|
|
数据缓冲平台 - 数据库连接
|
||
|
|
"""
|
||
|
|
from pathlib import Path
|
||
|
|
from sqlalchemy import create_engine
|
||
|
|
from sqlalchemy.orm import sessionmaker, declarative_base
|
||
|
|
from app.config import DB_PATH
|
||
|
|
|
||
|
|
# 确保数据目录存在
|
||
|
|
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
|
||
|
|
engine = create_engine(
|
||
|
|
f"sqlite:///{DB_PATH}",
|
||
|
|
connect_args={"check_same_thread": False},
|
||
|
|
pool_pre_ping=True,
|
||
|
|
)
|
||
|
|
|
||
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||
|
|
Base = declarative_base()
|
||
|
|
|
||
|
|
|
||
|
|
def get_db():
|
||
|
|
"""获取数据库会话"""
|
||
|
|
db = SessionLocal()
|
||
|
|
try:
|
||
|
|
yield db
|
||
|
|
finally:
|
||
|
|
db.close()
|