Skip to content
Myr

Merge pull request #1192 from nluchs/patch-3

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

Minor typos in sqlite3.rst

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents a25ee7e, 37fb051 · Browse files · 1 file+11 added−11 removed
This commit

State

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

Diff stats

1 file+11 added−11 removed

  • other:+11 added−11 removed

1 file+11 added−11 removed

modified:docs/patterns/sqlite3.rst+11 added-11 removed
1 unmodified line
23Using SQLite 3 with Flask4=========================56In Flask you can implement the opening of database connections on demand7and closing it when the context dies (usually at the end of the request)8easily.6In Flask you can easily implement the opening of database connections on 7demand, closing them when the context dies (usually at the end of the 8request).910Here is a simple example of how you can use SQLite 3 with Flask::1112 import sqlite3
12 unmodified lines
25 db = getattr(g, '_database', None)26 if db is not None:27 db.close()2829All the application needs to do in order to now use the database is having30an active application context (which is always true if there is an request29All the application needs to do in order to now use the database is have30an active application context (which is always true if there is a request31in flight) or to create an application context itself. At that point the32``get_db`` function can be used to get the current database connection.33Whenever the context is destroyed the database connection will be34terminated.
20 unmodified lines
55Connect on Demand56-----------------5758The upside of this approach (connecting on first use) is that this will59only opening the connection if truly necessary. If you want to use this59only open the connection if truly necessary. If you want to use this60code outside a request context you can use it in a Python shell by opening61the application context by hand::6263 with app.app_context():
6 unmodified lines
7071Now in each request handling function you can access `g.db` to get the72current open database connection. To simplify working with SQLite, a73row factory function is useful. It is executed for every result returned74from the database to convert the result. For instance in order to get75dictionaries instead of tuples this could be inserted into ``get_db``::74from the database to convert the result. For instance, in order to get75dictionaries instead of tuples, this could be inserted into ``get_db``::7677 def make_dicts(cursor, row):78 return dict((cursor.description[idx][0], value)79 for idx, value in enumerate(row))
12 unmodified lines
92 rv = cur.fetchall()93 cur.close()94 return (rv[0] if rv else None) if one else rv9596This handy little function in combination with a row factory makes working97with the database much more pleasant than it is by just using the raw98cursor and connection objects.96This handy little function, in combination with a row factory, makes 97working with the database much more pleasant than it is by just using the 98raw cursor and connection objects.99100Here is how you can use it::101102 for user in query_db('select * from users'):
36 unmodified lines