Skip to content
Myr

Use sqlite3.Row factory in Flaskr

Before adoption: this commit came in with the history the project adopted, so no landing records it.

As pointed out in issue #588 sqlite3.Row should be used instead of using casting to dict(). Also altered the "Easy Querying" Patterns example to include the more correct way to return rows as dicts. Did not touch Tutorial examples ("Views"), as these are not up to date with the current Flaskr code, and the "Show Entries" section points out the "Easy Querying" section on how to convert to a dict().

authored by Gennady Kovshenin · committed by Gennady Kovshenin · parent 2b885ce · Browse files · 2 files+9 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+9 added−2 removed

  • other:+9 added−2 removed

2 files+9 added−2 removed

modified:docs/patterns/sqlite3.rst+4 added
74 unmodified lines
75 for idx, value in enumerate(row))7677 db.row_factory = make_dicts7879Or even simpler::8081 db.row_factory = sqlite3.Row8283Additionally it is a good idea to provide a query function that combines84getting the cursor, executing and fetching the results::85 86 def query_db(query, args=(), one=False):
48 unmodified lines
modified:examples/flaskr/flaskr.py+5 added-2 removed
40 unmodified lines
41 current application context.42 """43 top = _app_ctx_stack.top44 if not hasattr(top, 'sqlite_db'):45 top.sqlite_db = sqlite3.connect(app.config['DATABASE'])45 sqlite_db = sqlite3.connect(app.config['DATABASE'])46 sqlite_db.row_factory = sqlite3.Row47 top.sqlite_db = sqlite_db4849 return top.sqlite_db505152@app.teardown_appcontext
7 unmodified lines
60@app.route('/')61def show_entries():62 db = get_db()63 cur = db.execute('select title, text from entries order by id desc')61 entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]64 entries = cur.fetchall()65 return render_template('show_entries.html', entries=entries)666768@app.route('/add', methods=['POST'])
36 unmodified lines