dbinit.rst
This file
Path
docs/tutorial/dbinit.rst
67 lines · 2.9 KiB
Last changed by
Changed the wording of some sentences there were difficult to understand.Reetta Vaahtorantabefore adoption0d648fa
History of this fileTake it
The walk stopped after 200 commits: lines older than 148c50a point at it.
Document url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 1–2why, one step back1.. _tutorial-dbinit:2Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 3why, one step back3Step 4: Creating The DatabaseDocument url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 4–5why, one step back4=============================5Changed the wording of some sentences there were difficult to understand.Reetta Vaahtorantabefore adoption0d648fa
lines 6–7why, one step back6As outlined earlier, Flaskr is a database powered application, and more7precisely, it is an application powered by a relational database system. SuchDocument url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 8–17why, one step back8systems need a schema that tells them how to store that information. So9before starting the server for the first time it's important to create10that schema.1112Such a schema can be created by piping the `schema.sql` file into the13`sqlite3` command as follows::1415 sqlite3 /tmp/flaskr.db < schema.sql1617The downside of this is that it requires the sqlite3 command to beChanged the wording of some sentences there were difficult to understand.Reetta Vaahtorantabefore adoption0d648fa
line 18why, one step back18installed which is not necessarily the case on every system. This also require that we provide the path to the database which can introduce errors. It's a good idea to add a function that initializes the databaseDocument url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 19–20why, one step back19for you to the application.20Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 21why, one step back21To do this we can create a function called `init_db` that initializes theChanged the wording of some sentences there were difficult to understand.Reetta Vaahtorantabefore adoption0d648fa
line 22why, one step back22database. Let me show you the code first. Just add this function belowRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 23why, one step back23the `connect_db` function in `flaskr.py`::Document url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 24–25why, one step back2425 def init_db():Merge pull request #858 from svieira/patch-2Daniel Neuhäuserbefore adoptionff59db1
line 26why, one step back26 with app.app_context():Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 27why, one step back27 db = get_db()Merge branch 'sprint-branch'Armin Ronacherbefore adoption29e63e0
line 28why, one step back28 with app.open_resource('schema.sql', mode='r') as f:Document url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 29–31why, one step back29 db.cursor().executescript(f.read())30 db.commit()31Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 32–46why, one step back32So what's happening here? Remember how we learned last chapter that the33application context is created every time a request comes in? Here we34don't have a request yet, so we need to create the application context by35hand. Without an application context the :data:`~flask.g` object does not36know yet to which application it becomes as there could be more than one!3738The ``with app.app_context()`` statement establishes the application39context for us. In the body of the with statement the :flask:`~flask.g`40object will be associated with ``app``. At the end of the with statement41the association is released and all teardown functions are executed. This42means that our database connection is disconnected after the commit.4344The :func:`~flask.Flask.open_resource` method of the application object45is a convenient helper function that will open a resource that the46application provides. This function opens a file from the resourceDocument url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 47–49why, one step back47location (your `flaskr` folder) and allows you to read from it. We are48using this here to execute a script on the database connection.49Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 50–53why, one step back50The connection object provided by SQLite can give us a cursor object.51On that cursor there is a method to execute a complete script. Finally we52only have to commit the changes. SQLite 3 and other transactional53databases will not commit unless you explicitly tell it to.Document url_for BuildError hook.Ron DuPlainbefore adoption148c50a
lines 54–66why, one step back5455Now it is possible to create a database by starting up a Python shell and56importing and calling that function::5758>>> from flaskr import init_db59>>> init_db()6061.. admonition:: Troubleshooting6263 If you get an exception later that a table cannot be found check that64 you did call the `init_db` function and that your table names are65 correct (singular vs. plural for example).66Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 67why, one step back67Continue with :ref:`tutorial-views`