creditrating/BM02_指标仓库/DataBase.py

29 lines
699 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)