Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MySQL Database Flask
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask web app MySQL runtime error TheTiger 8 10,612 Dec-04-2024, 03:18 AM
Last Post: TheTiger
  Webscrape using RPi and SQlite database, always write the last value in database Armond 0 1,545 Jul-19-2023, 09:11 PM
Last Post: Armond
  Flask: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 5,006 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 13,505 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Flask/non-flask database sharing MorganSamage 2 2,749 Feb-03-2023, 12:05 PM
Last Post: MorganSamage
  User Input to mySQL database newbie1 3 6,583 Aug-26-2020, 10:42 AM
Last Post: newbie1
  Flask export/upload database table in cvs/xlsx format steve87bg 4 11,557 Jun-19-2020, 01:46 PM
Last Post: steve87bg
  how to save the data from MySQL to CSV in Flask farah97 4 5,124 Jan-03-2020, 03:02 AM
Last Post: farah97
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 9,027 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  How to format a datetime MySQL database field to local using strftime() nikos 6 6,774 Feb-24-2019, 06:32 PM
Last Post: nikos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020