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 5,586 Dec-04-2024, 03:18 AM
Last Post: TheTiger
  Flask: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 3,683 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 10,051 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Flask/non-flask database sharing MorganSamage 2 2,043 Feb-03-2023, 12:05 PM
Last Post: MorganSamage
  User Input to mySQL database newbie1 3 5,590 Aug-26-2020, 10:42 AM
Last Post: newbie1
  Flask export/upload database table in cvs/xlsx format steve87bg 4 8,638 Jun-19-2020, 01:46 PM
Last Post: steve87bg
  how to save the data from MySQL to CSV in Flask farah97 4 4,082 Jan-03-2020, 03:02 AM
Last Post: farah97
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 8,243 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  How to format a datetime MySQL database field to local using strftime() nikos 6 5,296 Feb-24-2019, 06:32 PM
Last Post: nikos
  flask sqlite jinja accessing and updating database help pascale 5 5,648 Feb-11-2019, 03:49 PM
Last Post: pascale

Forum Jump:

User Panel Messages

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