sqlite dict cursors #588

Closed
ivan-kleshnin opened this Issue Sep 3, 2012 · 2 comments

Projects

None yet

3 participants

@ivan-kleshnin

There are whole sections based on assumption that sqlite has no built-in DictCursor behavior
http://flask.pocoo.org/docs/tutorial/views/#tutorial-views
http://flask.pocoo.org/docs/patterns/sqlite3/#easy-querying

Besides verbosity, by wrapping result in dict() we block transparent data passing to INSERT which waits for "tupled" format

Here is solution:
db = sqlite3.connect(app.config['DATABASE_NAME'])
db.row_factory = sqlite3.Row # trick!

While "print cur.fetchall()" still looks like tuples are here... don't worry, it supports key access:

for item in cur.fetchall():
print item['title'] # works!

So you can easily remove those custom wrappers which i saw copied here and there...

Owner

Sounds like a good idea. Do you want to update the docs and send me a pull request?

@soulseekah soulseekah added a commit to soulseekah/flask that referenced this issue Nov 5, 2012
@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
Contributor
DasIch commented Jun 19, 2013

Fixed with #625

@DasIch DasIch closed this Jun 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment