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
Provenance
- Commit
60fcc05- Tree
4eaff0b- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
a25ee7e- Parent 2
37fb051
1 file changed+11 added−11 removed
modified:docs/patterns/sqlite3.rst+11 added-11 removed
1 unmodified line2 3 Using SQLite 3 with Flask4 =========================5 6-In Flask you can implement the opening of database connections on demand7-and closing it when the context dies (usually at the end of the request)8-easily.6+In Flask you can easily implement the opening of database connections on 7+demand, closing them when the context dies (usually at the end of the 8+request).9 10 Here is a simple example of how you can use SQLite 3 with Flask::11 12 import sqlite312 unmodified lines25 db = getattr(g, '_database', None)26 if db is not None:27 db.close()28 29-All the application needs to do in order to now use the database is having30-an active application context (which is always true if there is an request29+All the application needs to do in order to now use the database is have30+an active application context (which is always true if there is a request31 in flight) or to create an application context itself. At that point the32 ``get_db`` function can be used to get the current database connection.33 Whenever the context is destroyed the database connection will be34 terminated.20 unmodified lines55 Connect on Demand56 -----------------57 58 The upside of this approach (connecting on first use) is that this will59-only opening the connection if truly necessary. If you want to use this59+only open the connection if truly necessary. If you want to use this60 code outside a request context you can use it in a Python shell by opening61 the application context by hand::62 63 with app.app_context():6 unmodified lines70 71 Now in each request handling function you can access `g.db` to get the72 current open database connection. To simplify working with SQLite, a73 row factory function is useful. It is executed for every result returned74-from the database to convert the result. For instance in order to get75-dictionaries instead of tuples this could be inserted into ``get_db``::74+from the database to convert the result. For instance, in order to get75+dictionaries instead of tuples, this could be inserted into ``get_db``::76 77 def make_dicts(cursor, row):78 return dict((cursor.description[idx][0], value)79 for idx, value in enumerate(row))12 unmodified lines92 rv = cur.fetchall()93 cur.close()94 return (rv[0] if rv else None) if one else rv95 96-This handy little function in combination with a row factory makes working97-with the database much more pleasant than it is by just using the raw98-cursor and connection objects.96+This handy little function, in combination with a row factory, makes 97+working with the database much more pleasant than it is by just using the 98+raw cursor and connection objects.99 100 Here is how you can use it::101 102 for user in query_db('select * from users'):36 unmodified lines