When looking at the use of SQLite in the Flask documentation, the tutorial and dedicated SQLite doc section differ. In particular the init_db function:
Tutorial
def init_db():
"""Clear existing data and create new tables."""
db = get_db()
with current_app.open_resource("schema.sql") as f:
db.executescript(f.read().decode("utf8"))
Dedicated doc
def init_db():
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
- In the tutorial no context is used while in the dedicated documentation there is the statement
with app.app_context():. - In the tutorial the db is initialized with
db.executescript(...)while in the doc it isdb.cursor().executescript(...) - In the tutorial there is no database commit, while the documentation use
db.commitafter reading the schema
So the associated questions are:
- Should a context be used when initializing the database ?
- Should one use a cursor to the database or the database directly ?
- Should the transaction be committed or not after executing the schema ?