Skip to content
Myr

dbcon.rst

This file

Path

docs/tutorial/dbcon.rst

77 lines · 3.3 KiB

Last changed by

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

History of this file

Take it

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

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 1–6why, one step back
1.. _tutorial-dbcon:
23Step 3: Database Connections4----------------------------56We have created a function for establishing a database connection with

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

line 7why, one step back
7`connect_db`, but by itself, that's not particularly useful. Creating and

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 8–9why, one step back
8closing database connections all the time is very inefficient, so we want
9to keep it around for longer. Because database connections encapsulate a

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

lines 10–11why, one step back
10transaction, we also need to make sure that only one request at the time
11uses the connection. How can we elegantly do that with Flask?

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

line 12why, one step back
12

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

line 13why, one step back
13This is where the application context comes into play, so let's start

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 14–16why, one step back
14there.
1516Flask provides us with two contexts: the application context and the

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

lines 17–18why, one step back
17request context. For the time being, all you have to know is that there
18are special variables that use these. For instance, the

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 19–23why, one step back
19:data:`~flask.request` variable is the request object associated with
20the current request, whereas :data:`~flask.g` is a general purpose21variable associated with the current application context. We will go into22the details of this a bit later.23

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

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

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 25–27why, one step back
25safely on the :data:`~flask.g` object.
2627So 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 28–29why, one step back
28function. The first time the function is called, it will create a database
29connection for the current context, and successive calls will return the

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 30–40why, one step back
30already established connection::
3132 def get_db():33 """Opens a new database connection if there is none yet for the34 current application context.35 """36 if not hasattr(g, 'sqlite_db'):37 g.sqlite_db = connect_db()38 return g.sqlite_db3940So now we know how to connect, but how do we properly disconnect? For

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

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

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

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

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

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

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

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

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

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

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 56–76why, one step back
56is passed to the teardown function.
5758Curious about what these contexts mean? Have a look at the59:ref:`app-context` documentation to learn more.6061Continue to :ref:`tutorial-dbinit`.6263.. hint:: Where do I put this code?6465 If you've been following along in this tutorial, you might be wondering66 where to put the code from this step and the next. A logical place is to67 group these module-level functions together, and put your new68 ``get_db`` and ``close_db`` functions below your existing69 ``connect_db`` function (following the tutorial line-by-line).7071 If you need a moment to find your bearings, take a look at how the `example72 source`_ is organized. In Flask, you can put all of your application code73 into a single Python module. You don't have to, and if your app :ref:`grows74 larger <larger-applications>`, it's a good idea not to.7576.. _example source:

Change to Pallets projectJosé Carlos Garcíabefore adoption567fff9

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