Inconsistent use of SQLite database in Flask documentation: context, cursor and commit

Viewed 14

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()
  1. In the tutorial no context is used while in the dedicated documentation there is the statement with app.app_context():.
  2. In the tutorial the db is initialized with db.executescript(...) while in the doc it is db.cursor().executescript(...)
  3. In the tutorial there is no database commit, while the documentation use db.commit after reading the schema

So the associated questions are:

  1. Should a context be used when initializing the database ?
  2. Should one use a cursor to the database or the database directly ?
  3. Should the transaction be committed or not after executing the schema ?
0 Answers
Related