dbcon.rst
This file
Path
docs/tutorial/dbcon.rst
78 lines · 3.4 KiB
Last changed by
resolve merge conflictsKenneth Reitzbefore adoption31174fe
History of this fileTake it
The walk stopped after 200 commits: lines older than 817b72d point at it.
Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 1–2why, one step back1.. _tutorial-dbcon:2resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 3why, one step back3Step 4: Database ConnectionsRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 4–5why, one step back4----------------------------5resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 6–7why, one step back6Let's continue building our code in the ``flaskr.py`` file.7(Scroll to the end of the page for more about project layout.)Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 8why, one step back8resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 9–15why, one step back9You currently have a function for establishing a database connection with10`connect_db`, but by itself, it is not particularly useful. Creating and11closing database connections all the time is very inefficient, so you will12need to keep it around for longer. Because database connections13encapsulate a transaction, you will need to make sure that only one14request at a time uses the connection. An elegant way to do this is by15utilizing the *application context*.Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 16why, one step back16resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 17–18why, one step back17Flask provides two contexts: the *application context* and the18*request context*. For the time being, all you have to know is that thereMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 19why, one step back19are special variables that use these. For instance, theRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 20–21why, one step back20:data:`~flask.request` variable is the request object associated with21the current request, whereas :data:`~flask.g` is a general purposeresolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 22–23why, one step back22variable associated with the current application context. The tutorial23will cover some more details of this later on.Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
line 24why, one step back24Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 25why, one step back25For the time being, all you have to know is that you can store informationRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 26–28why, one step back26safely on the :data:`~flask.g` object.2728So when do you put it on there? To do that you can make a helperMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 29–30why, one step back29function. The first time the function is called, it will create a database30connection for the current context, and successive calls will return theRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 31–40why, one step back31already established connection::3233 def get_db():34 """Opens a new database connection if there is none yet for the35 current application context.36 """37 if not hasattr(g, 'sqlite_db'):38 g.sqlite_db = connect_db()39 return g.sqlite_db40resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 41why, one step back41Now you know how to connect, but how can you properly disconnect? ForMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 42why, one step back42that, Flask provides us with the :meth:`~flask.Flask.teardown_appcontext`Removed deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 43–51why, one step back43decorator. It's executed every time the application context tears down::4445 @app.teardown_appcontext46 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 calledMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 52–53why, one step back52every time the app context tears down. What does this mean?53Essentially, the app context is created before the request comes in and isRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 54–55why, one step back54destroyed (torn down) whenever the request finishes. A teardown can55happen because of two reasons: either everything went well (the errorMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 56why, one step back56parameter will be ``None``) or an exception happened, in which case the errorRemoved deprecated module functionalityArmin Ronacherbefore adoption817b72d
lines 57–77why, one step back57is 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`.6364.. 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 new69 ``get_db`` and ``close_db`` functions below your existing70 ``connect_db`` function (following the tutorial line-by-line).7172 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:resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 78why, one step back78 https://github.com/pallets/flask/tree/master/examples/flaskr/