Python Forum
MySQL Database Flask - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: MySQL Database Flask (/thread-21527.html)



MySQL Database Flask - maurosmartins - Oct-03-2019

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.