zhenxun_bot/services/db_context.py

26 lines
589 B
Python
Raw Normal View History

2021-05-20 18:37:51 +08:00
from gino import Gino
from .log import logger
from configs.config import bind, sql_name, user, password, address, port, database
# 全局数据库连接对象
db = Gino()
async def init():
2021-11-04 16:11:50 +08:00
i_bind = bind
2021-06-23 15:57:03 +08:00
if not i_bind:
2021-05-20 18:37:51 +08:00
i_bind = f"{sql_name}://{user}:{password}@{address}:{port}/{database}"
try:
await db.set_bind(i_bind)
await db.gino.create_all()
logger.info(f'Database loaded successfully!')
except Exception as e:
raise Exception(f'数据库连接错误.... e: {e}')
async def disconnect():
await db.pop_bind().close()