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')
-
复制代码 |