29 lines
699 B
Python
29 lines
699 B
Python
from sqlalchemy import create_engine
|
||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||
|
||
from .Configs import *
|
||
|
||
|
||
# MySQL
|
||
engine = create_engine(MYSQL_CONFIG) # 创建数据库引擎实例
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # 定义数据库会话类
|
||
Base = declarative_base() # 定义数据库模型基类
|
||
|
||
|
||
def get_mysqldb():
|
||
"""
|
||
定义MySQL数据库依赖函数
|
||
"""
|
||
db = SessionLocal()
|
||
try:
|
||
yield db
|
||
finally:
|
||
db.close()
|
||
|
||
|
||
def generate_mysql_tables():
|
||
"""
|
||
根据Sqlalchemy的数据库模型定义,将数据库模型生成数据库中的表结构
|
||
"""
|
||
Base.metadata.create_all(bind=engine)
|