Python Forum

Full Version: MySQL Database Flask
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

I'm just starting using Flask and I have some doubts where and how I should make my DB connection.

my app is currently organized as follows:
/
├── app/
│ ├── forms.py
│ ├── routes.py
│ ├── templates
│ └── ...
│ └── static
│ └── ...
├── flaskapp.py

on the routes.py I have a "index" and a "chart" route each one needs access to the DB (I'm using mysql.connector). Currently I'm doing the following at each route:

    mydb = mysql.connector.connect(
        host="localhost",
        user="foo",
        passwd="bar",
        database="foobar")
    mycursor = mydb.cursor()
I don't think it is correct to be doing this each time I need to use the DB. What should the correct way be? can you please provide an example?

looking forward your reply,
Best regards, Mauro.