0

I have two databases say xyz and abc. The thing is I want to have two MySQL instances using one Flask app. This is how I initialize one MySQL Instance:

app = Flask(__name__)
app.config['MYSQL_HOST']='localhost'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']='abcd'
app.config['MYSQL_DB'] = 'xyz'
mysql_xyz = MySQL(app)

I have another database lets say 'abc' when I am trying to overwrite 'MYSQL_DB' by:

app.config['MYSQL_DB'] = 'abc'
mysql_abc = MySQL(app)

I am getting the following error:

Debugging middleware caught exception in streamed response at a point where response headers were already sent. MySQLdb._exceptions.OperationalError: (2006, '')

However, another alternative is to create a different app altogether under the same script file app.py

app1 = Flask(__name__)
app1.config['MYSQL_HOST']='localhost'
app1.config['MYSQL_USER']='root'
app1.config['MYSQL_PASSWORD']='abcd'
app1.config['MYSQL_DB'] = 'abc'
mysql_abc = MySQL(app1)

and this works fine. can anyone please let me know how can I overwrite the value of 'MYSQL_DB' config and create another mysql instance using only one app variable.

1 Answer 1

0

You most likely want to create multiple tables within a single database, instead of creating multiple databases.

Else, these will help: https://flask-appbuilder.readthedocs.io/en/latest/multipledbs.html Configuring Flask-SQLAlchemy to use multiple databases with Flask-Restless

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.