Skip to content
Myr

dbcon.rst

This file

Path

docs/tutorial/dbcon.rst

75 lines · 3.3 KiB

Last changed by

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

History of this file

Take it

The walk stopped after 200 commits: lines older than a25ee7e point at it.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 1–2why, one step back
1.. _tutorial-dbcon:
2

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 3why, one step back
3Step 4: Database Connections

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 4–5why, one step back
4----------------------------
5

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 6–12why, one step back
6You now have a function for establishing a database connection with
7`connect_db`, but by itself, it is not particularly useful. Creating and8closing database connections all the time is very inefficient, so you will9need to keep it around for longer. Because database connections10encapsulate a transaction, you will need to make sure that only one11request at a time uses the connection. An elegant way to do this is by12utilizing the *application context*.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 13why, one step back
13

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 14–15why, one step back
14Flask provides two contexts: the *application context* and the
15*request context*. For the time being, all you have to know is that there

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 16why, one step back
16are special variables that use these. For instance, the

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 17–18why, one step back
17:data:`~flask.request` variable is the request object associated with
18the current request, whereas :data:`~flask.g` is a general purpose

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 19–20why, one step back
19variable associated with the current application context. The tutorial
20will cover some more details of this later on.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 21why, one step back
21

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 22why, one step back
22For the time being, all you have to know is that you can store information

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 23–25why, one step back
23safely on the :data:`~flask.g` object.
2425So when do you put it on there? To do that you can make a helper

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

lines 26–27why, one step back
26function. The first time the function is called, it will create a database
27connection for the current context, and successive calls will return the

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 28–37why, one step back
28already established connection::
2930 def get_db():31 """Opens a new database connection if there is none yet for the32 current application context.33 """34 if not hasattr(g, 'sqlite_db'):35 g.sqlite_db = connect_db()36 return g.sqlite_db37

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 38why, one step back
38Now you know how to connect, but how can you properly disconnect? For

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 39why, one step back
39that, Flask provides us with the :meth:`~flask.Flask.teardown_appcontext`

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 40–48why, one step back
40decorator. It's executed every time the application context tears down::
4142 @app.teardown_appcontext43 def close_db(error):44 """Closes the database again at the end of the request."""45 if hasattr(g, 'sqlite_db'):46 g.sqlite_db.close()4748Functions marked with :meth:`~flask.Flask.teardown_appcontext` are called

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

lines 49–50why, one step back
49every time the app context tears down. What does this mean?
50Essentially, the app context is created before the request comes in and is

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 51–52why, one step back
51destroyed (torn down) whenever the request finishes. A teardown can
52happen because of two reasons: either everything went well (the error

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 53why, one step back
53parameter will be ``None``) or an exception happened, in which case the error

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 54–74why, one step back
54is passed to the teardown function.
5556Curious about what these contexts mean? Have a look at the57:ref:`app-context` documentation to learn more.5859Continue to :ref:`tutorial-dbinit`.6061.. hint:: Where do I put this code?6263 If you've been following along in this tutorial, you might be wondering64 where to put the code from this step and the next. A logical place is to65 group these module-level functions together, and put your new66 ``get_db`` and ``close_db`` functions below your existing67 ``connect_db`` function (following the tutorial line-by-line).6869 If you need a moment to find your bearings, take a look at how the `example70 source`_ is organized. In Flask, you can put all of your application code71 into a single Python module. You don't have to, and if your app :ref:`grows72 larger <larger-applications>`, it's a good idea not to.7374.. _example source:

Change to Pallets projectJosé Carlos Garcíabefore adoption567fff9

line 75why, one step back
75 https://github.com/pallets/flask/tree/master/examples/flaskr/