Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use sqlite3.Row factory in Flaskr (#588) #625

Merged
merged 1 commit into from

2 participants

@soulseekah

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().

@soulseekah soulseekah Use sqlite3.Row factory in Flaskr
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().
82b29c0
@kennethreitz
Collaborator

thanks!

@kennethreitz kennethreitz merged commit 3fdcefb into mitsuhiko:master

1 check passed

Details default The Travis build passed
@DasIch DasIch referenced this pull request
Closed

sqlite dict cursors #588

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 5, 2012
  1. @soulseekah

    Use sqlite3.Row factory in Flaskr

    soulseekah authored
    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().
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 2 deletions.
  1. +4 −0 docs/patterns/sqlite3.rst
  2. +5 −2 examples/flaskr/flaskr.py
View
4 docs/patterns/sqlite3.rst
@@ -76,6 +76,10 @@ dictionaries instead of tuples this can be used::
db.row_factory = make_dicts
+Or even simpler::
+
+ db.row_factory = sqlite3.Row
+
Additionally it is a good idea to provide a query function that combines
getting the cursor, executing and fetching the results::
View
7 examples/flaskr/flaskr.py
@@ -42,7 +42,10 @@ def get_db():
"""
top = _app_ctx_stack.top
if not hasattr(top, 'sqlite_db'):
- top.sqlite_db = sqlite3.connect(app.config['DATABASE'])
+ sqlite_db = sqlite3.connect(app.config['DATABASE'])
+ sqlite_db.row_factory = sqlite3.Row
+ top.sqlite_db = sqlite_db
+
return top.sqlite_db
@@ -58,7 +61,7 @@ def close_db_connection(exception):
def show_entries():
db = get_db()
cur = db.execute('select title, text from entries order by id desc')
- entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
+ entries = cur.fetchall()
return render_template('show_entries.html', entries=entries)
Something went wrong with that request. Please try again.