3

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 .

rsev4292340
  • 599
  • 1
  • 4
  • 7

2 Answers2

0

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 :)

stefano manzi
  • 109
  • 2
  • 8
0
cursor.execute("SELECT * FROM db_name.db_table")
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 26 '23 at 01:12
  • 2
    But, also, how is this different than what @stefano already suggested last year? – Jeremy Caney Jan 26 '23 at 01:12
  • Because I send one query. – Jessy James Jan 26 '23 at 13:54