Oct-03-2019, 10:56 AM
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:
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.
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:
1 2 3 4 5 6 |
mydb = mysql.connector.connect( host = "localhost" , user = "foo" , passwd = "bar" , database = "foobar" ) mycursor = mydb.cursor() |
looking forward your reply,
Best regards, Mauro.