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


Messages In This Thread
MySQL Database Flask - by maurosmartins - Oct-03-2019, 10:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 2,120 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 4,713 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Flask/non-flask database sharing MorganSamage 2 1,189 Feb-03-2023, 12:05 PM
Last Post: MorganSamage
  User Input to mySQL database newbie1 3 4,193 Aug-26-2020, 10:42 AM
Last Post: newbie1
  Flask export/upload database table in cvs/xlsx format steve87bg 4 6,856 Jun-19-2020, 01:46 PM
Last Post: steve87bg
  how to save the data from MySQL to CSV in Flask farah97 4 2,938 Jan-03-2020, 03:02 AM
Last Post: farah97
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 7,161 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  How to format a datetime MySQL database field to local using strftime() nikos 6 3,794 Feb-24-2019, 06:32 PM
Last Post: nikos
  flask sqlite jinja accessing and updating database help pascale 5 4,170 Feb-11-2019, 03:49 PM
Last Post: pascale
  how i save the html form to flask database mebaysan 1 7,304 Feb-07-2019, 12:56 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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