Connect to multiple database from Flask using flask_mysqldb

Viewed 429

I have a Flask application that connect to one db(db1) using flask-mysqldb module

app.config['MYSQL_HOST'] = 'host1'
app.config['MYSQL_USER'] = 'user1'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'db1'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)

I want to connect to one more db that is db2 in the same application. How can I be able to achieve that? I could connect to other db using MySQLdb , But I want to connect with flask-mysqldb .

1 Answers

Just change databse before doing your query to the second db

cursor.execute("USE db_name")
cursor.execute("SELECT * FROM...")

Hope it is still useful after such a long time :)

Related