Skip to content
Myr

Merge pull request #625 from soulseekah/master

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

Use sqlite3.Row factory in Flaskr (#588)

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 17fc38c, 82b29c0 · 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