Reputation: 6307
I'm developing app in Flask and it requires DB, so what I have is I do:
app = Flask(__name__)
@app.before_request
def init_db_connection:
# here I connect to my DB
@app.teardown_request
def destroy_db(exception):
# here I destroy database connection
On the development server (app.run()) this is not the best place to initialize the database I guess, because also DB will get initialized even it the request comes for the static file. In production I can have a separate web server serving static files, so it shouldn't be a problem.
But still I'm thinking if this is right way to initialize DB or it is better for example to initialize DB in Blueprint which is used in that moment? Just want to know the best practice and how you guys doing this :)
Upvotes: 19
Views: 14311
Reputation: 51
Flask Application can be initialised by using inbuilt SQLAlchemy modules.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# create the extension
db = SQLAlchemy()
# create the app
app = Flask(__name__)
# configure the SQLite database
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
# initialize the app with the extension
db.init_app(app)
Note: The db object gives you access to the db.Model class to define models, and the db.session to execute queries.
Reference: flask_documentation
Upvotes: 1
Reputation: 1145
Here are a few options:
init_db_connection
method, you can check the route and if it is a static route or route where you don't think the db is necessary then don't do the db init.@app.route("/example_using_db")
def example_using_db():
with MyDBCursor() as cur: # context manager which connects to db and provides cursor
data = cur.execute("SELECT * from EXAMPLE").fetchall()
Depending on your library, you may be able to get a context manager like above. If not you can either write one or put your code which connects to the db in a try/finally block so that you release the connection if there is an exception.
Upvotes: -2