Browse Source

Merge pull request #1192 from nluchs/patch-3

Minor typos in sqlite3.rst
pull/1195/head
Markus Unterwaditzer 10 years ago
parent
commit
60fcc05c47
  1. 22
      docs/patterns/sqlite3.rst

22
docs/patterns/sqlite3.rst

@ -3,9 +3,9 @@
Using SQLite 3 with Flask Using SQLite 3 with Flask
========================= =========================
In Flask you can implement the opening of database connections on demand In Flask you can easily implement the opening of database connections on
and closing it when the context dies (usually at the end of the request) demand, closing them when the context dies (usually at the end of the
easily. request).
Here is a simple example of how you can use SQLite 3 with Flask:: Here is a simple example of how you can use SQLite 3 with Flask::
@ -26,8 +26,8 @@ Here is a simple example of how you can use SQLite 3 with Flask::
if db is not None: if db is not None:
db.close() db.close()
All the application needs to do in order to now use the database is having All the application needs to do in order to now use the database is have
an active application context (which is always true if there is an request an active application context (which is always true if there is a request
in flight) or to create an application context itself. At that point the in flight) or to create an application context itself. At that point the
``get_db`` function can be used to get the current database connection. ``get_db`` function can be used to get the current database connection.
Whenever the context is destroyed the database connection will be Whenever the context is destroyed the database connection will be
@ -56,7 +56,7 @@ Connect on Demand
----------------- -----------------
The upside of this approach (connecting on first use) is that this will The upside of this approach (connecting on first use) is that this will
only opening the connection if truly necessary. If you want to use this only open the connection if truly necessary. If you want to use this
code outside a request context you can use it in a Python shell by opening code outside a request context you can use it in a Python shell by opening
the application context by hand:: the application context by hand::
@ -71,8 +71,8 @@ Easy Querying
Now in each request handling function you can access `g.db` to get the Now in each request handling function you can access `g.db` to get the
current open database connection. To simplify working with SQLite, a current open database connection. To simplify working with SQLite, a
row factory function is useful. It is executed for every result returned row factory function is useful. It is executed for every result returned
from the database to convert the result. For instance in order to get from the database to convert the result. For instance, in order to get
dictionaries instead of tuples this could be inserted into ``get_db``:: dictionaries instead of tuples, this could be inserted into ``get_db``::
def make_dicts(cursor, row): def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value) return dict((cursor.description[idx][0], value)
@ -93,9 +93,9 @@ getting the cursor, executing and fetching the results::
cur.close() cur.close()
return (rv[0] if rv else None) if one else rv return (rv[0] if rv else None) if one else rv
This handy little function in combination with a row factory makes working This handy little function, in combination with a row factory, makes
with the database much more pleasant than it is by just using the raw working with the database much more pleasant than it is by just using the
cursor and connection objects. raw cursor and connection objects.
Here is how you can use it:: Here is how you can use it::

Loading…
Cancel
Save