无限星辰工作室-客户无限互联网动力之源
标题:
Flask+SQLAlchemy多Mysql库配置
[打印本页]
作者:
crx349
时间:
2020-7-2 23:47
标题:
Flask+SQLAlchemy多Mysql库配置
config.py
# mysql
MYSQL = {"HOST": "127.0.0.1",
'PORT': "3306",
'USER': "xmspace.net",
'PASSWD': "xmspace.net",
'DB': "xmspace"}
MYSQL_BIND ={
'devops' : 'mysql://{}:{}@{}:{}/{}'.format('xmspace','xmspace.net','127.0.0.1','3306', 'xmspace'),
'game' : 'mysql://{}:{}@{}:{}/{}'.format('xmspace1','xmspace.net','127.0.0.1','3306', 'xmspace1'),
}
复制代码
app.py
def init_db(app):
'''
加载数据库
:param app:
:return:
'''
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{}:{}@{}:{}/{}'.format(config.MYSQL['USER'],
config.MYSQL['PASSWD'],
config.MYSQL['HOST'],
config.MYSQL['PORT'], config.MYSQL['DB'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # 跟踪对象的修改,在本例中用不到调高运行效率,所以设置为False
app.config['SQLALCHEMY_BINDS'] = config.MYSQL_BIND
app.config['SQLALCHEMY_ECHO'] = True
db.init_app(app)
复制代码
实际models下使用
class Xmspace(db.model):
__bind_key__ = 'game' #选择连接池
__tablename__ = 'xmspace'
Id = db.Column(db.BigInteger, primary_key=True, info='ID')
复制代码
欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://xmspace.net/)
Powered by Discuz! X3.4