3
3
Using SQLite 3 with Flask
4
4
=========================
5
5
6
- In Flask you can implement the opening of database connections on demand
7
- 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
9
10
10
Here is a simple example of how you can use SQLite 3 with Flask::
11
11
@@ -26,8 +26,8 @@ Here is a simple example of how you can use SQLite 3 with Flask::
26
26
if db is not None:
27
27
db.close()
28
28
29
- All the application needs to do in order to now use the database is having
30
- an active application context (which is always true if there is an request
29
+ All the application needs to do in order to now use the database is have
30
+ an active application context (which is always true if there is a request
31
31
in flight) or to create an application context itself. At that point the
32
32
``get_db `` function can be used to get the current database connection.
33
33
Whenever the context is destroyed the database connection will be
@@ -56,7 +56,7 @@ Connect on Demand
56
56
-----------------
57
57
58
58
The upside of this approach (connecting on first use) is that this will
59
- only opening the connection if truly necessary. If you want to use this
59
+ only open the connection if truly necessary. If you want to use this
60
60
code outside a request context you can use it in a Python shell by opening
61
61
the application context by hand::
62
62
@@ -71,8 +71,8 @@ Easy Querying
71
71
Now in each request handling function you can access `g.db ` to get the
72
72
current open database connection. To simplify working with SQLite, a
73
73
row factory function is useful. It is executed for every result returned
74
- from the database to convert the result. For instance in order to get
75
- dictionaries instead of tuples this could be inserted into ``get_db ``::
74
+ from the database to convert the result. For instance, in order to get
75
+ dictionaries instead of tuples, this could be inserted into ``get_db ``::
76
76
77
77
def make_dicts(cursor, row):
78
78
return dict((cursor.description[idx][0], value)
@@ -93,9 +93,9 @@ getting the cursor, executing and fetching the results::
93
93
cur.close()
94
94
return (rv[0] if rv else None) if one else rv
95
95
96
- This handy little function in combination with a row factory makes working
97
- with the database much more pleasant than it is by just using the raw
98
- 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
99
100
100
Here is how you can use it::
101
101
0 commit comments