Skip to content
Myr

dbinit.rst

This file

Path

docs/tutorial/dbinit.rst

69 lines · 2.9 KiB

Last changed by

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

History of this file

Take it

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

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 3why, one step back
3Step 4: Creating The Database

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

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

Changed the wording of some sentences there were difficult to understand.Reetta Vaahtorantabefore adoption0d648fa

lines 6–7why, one step back
6As outlined earlier, Flaskr is a database powered application, and more
7precisely, it is an application powered by a relational database system. Such

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

lines 8–17why, one step back
8systems need a schema that tells them how to store that information. So
9before 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 be

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

lines 18–20why, one step back
18installed which is not necessarily the case on every system. This also
19require that we provide the path to the database which can introduce20errors. It's a good idea to add a function that initializes the database

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

lines 21–22why, one step back
21for you to the application.
22

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 23why, one step back
23To do this we can create a function called `init_db` that initializes the

Changed the wording of some sentences there were difficult to understand.Reetta Vaahtorantabefore adoption0d648fa

line 24why, one step back
24database. Let me show you the code first. Just add this function below

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 25why, one step back
25the `connect_db` function in `flaskr.py`::

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

lines 26–27why, one step back
26
27 def init_db():

Merge pull request #858 from svieira/patch-2Daniel Neuhäuserbefore adoptionff59db1

line 28why, one step back
28 with app.app_context():

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 29why, one step back
29 db = get_db()

Merge branch 'sprint-branch'Armin Ronacherbefore adoption29e63e0

line 30why, one step back
30 with app.open_resource('schema.sql', mode='r') as f:

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

lines 31–33why, one step back
31 db.cursor().executescript(f.read())
32 db.commit()33

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 34–48why, one step back
34So what's happening here? Remember how we learned last chapter that the
35application context is created every time a request comes in? Here we36don't have a request yet, so we need to create the application context by37hand. Without an application context the :data:`~flask.g` object does not38know yet to which application it becomes as there could be more than one!3940The ``with app.app_context()`` statement establishes the application41context for us. In the body of the with statement the :flask:`~flask.g`42object will be associated with ``app``. At the end of the with statement43the association is released and all teardown functions are executed. This44means that our database connection is disconnected after the commit.4546The :func:`~flask.Flask.open_resource` method of the application object47is a convenient helper function that will open a resource that the48application provides. This function opens a file from the resource

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

lines 49–51why, one step back
49location (your `flaskr` folder) and allows you to read from it. We are
50using this here to execute a script on the database connection.51

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 52–55why, one step back
52The connection object provided by SQLite can give us a cursor object.
53On that cursor there is a method to execute a complete script. Finally we54only have to commit the changes. SQLite 3 and other transactional55databases will not commit unless you explicitly tell it to.

Removed outdated sentence in the testing docsArmin Ronacherbefore adoption22af78a

lines 56–68why, one step back
56
57Now it is possible to create a database by starting up a Python shell and58importing and calling that function::5960>>> from flaskr import init_db61>>> init_db()6263.. admonition:: Troubleshooting6465 If you get an exception later that a table cannot be found check that66 you did call the `init_db` function and that your table names are67 correct (singular vs. plural for example).68

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 69why, one step back
69Continue with :ref:`tutorial-views`