简体   繁体   中英

Add second MySQL connection in python(flask)

how can i add another sql connection in python(i'm using flask)

I have such a code in init.py:

from flask import Flask
from flaskext.mysql import MySQL

app = Flask(__name__)
wsgi_app = app.wsgi_app

app.config['MYSQL_DATABASE_HOST'] = 'remotemysql.com'
app.config['MYSQL_DATABASE_USER'] = '-'
app.config['MYSQL_DATABASE_PASSWORD'] = '-'
app.config['MYSQL_DATABASE_DB'] = '-'

db = MySQL()
db.init_app(app)

from .libs import controllers

So how can i add another connection for example promoDB with another connection settings?

Never tried for the same DB, but you can refer https://flask-sqlalchemy.palletsprojects.com/en/2.x/binds/

SQLALCHEMY_DATABASE_URI = 'postgres://localhost/main'
SQLALCHEMY_BINDS = {
    'users':        'mysqldb://localhost/users',
    'appmeta':      'sqlite:////path/to/appmeta.db'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM