You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
report_app/app/entity/database/session.py

21 lines
662 B
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# 数据库连接 URL根据实际情况修改用户名、密码、主机、端口和数据库名
# SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:ultrapower123@localhost:3306/ultra_me"
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:ngsk0809cruise@localhost:33306/gcgj"
# 创建数据库引擎
engine = create_engine(SQLALCHEMY_DATABASE_URL)
# 创建会话工厂
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# 依赖函数,用于获取数据库会
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()