Skip to content
Myr

dbcon.rst

This file

Path

docs/tutorial/dbcon.rst

78 lines · 3.3 KiB

Last changed by

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

History of this file

Take it

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 3–4why, one step back
3Step 3: Database Connections
4----------------------------

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 5why, one step back
5

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 6why, one step back
6We have created a function for establishing a database connection with

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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?

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 12why, one step back
12

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 14why, one step back
14there.

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 15why, one step back
15

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 16why, one step back
16Flask provides us with two contexts: the application context and the

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 19–22why, 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.

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 23why, one step back
23

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Merge pull request #886 from talam/patch-1Daniel Neuhäuserbefore adoptioncab514e

line 25why, one step back
25safely on the :data:`~flask.g` object.

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 26why, one step back
26

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 27why, one step back
27So when do you put it on there? To do that you can make a helper

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 30why, one step back
30already established connection::

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 31why, one step back
31

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 32–38why, one step back
32 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_db

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

lines 39–40why, one step back
39
40

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 41why, one step back
41So now we know how to connect, but how do we properly disconnect? For

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 43why, one step back
43decorator. It's executed every time the application context tears down::

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 44why, one step back
44

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 45–51why, one step back
45 @app.teardown_appcontext
46 def close_db(error):47 """Closes the database again at the end of the request."""48 if hasattr(g, 'sqlite_db'):49 g.sqlite_db.close()5051Functions marked with :meth:`~flask.Flask.teardown_appcontext` are called

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Merge pull request #1096 from slater/patch-1Kenneth Reitzbefore adoption0ce47a3

line 54why, one step back
54destroyed (torn down) whenever the request finishes. A teardown can

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 55why, one step back
55happen because of two reasons: either everything went well (the error

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 57–63why, one step back
57is passed to the teardown function.
5859Curious about what these contexts mean? Have a look at the60:ref:`app-context` documentation to learn more.6162Continue to :ref:`tutorial-dbinit`.63

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

lines 64–68why, one step back
64.. hint:: Where do I put this code?
6566 If you've been following along in this tutorial, you might be wondering67 where to put the code from this step and the next. A logical place is to68 group these module-level functions together, and put your new

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 69why, one step back
69 ``get_db`` and ``close_db`` functions below your existing

Merge pull request #886 from talam/patch-1Daniel Neuhäuserbefore adoptioncab514e

line 70why, one step back
70 ``connect_db`` function (following the tutorial line-by-line).

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

lines 71–77why, one step back
71
72 If you need a moment to find your bearings, take a look at how the `example73 source`_ is organized. In Flask, you can put all of your application code74 into a single Python module. You don't have to, and if your app :ref:`grows75 larger <larger-applications>`, it's a good idea not to.7677.. _example source:

Merge pull request #1132 from nicorevin/update_linksDaniel Neuhäuserbefore adoption8d3ee01

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