Skip to content
Myr

dbinit.rst

This file

Path

docs/tutorial/dbinit.rst

65 lines · 2.7 KiB

Last changed by

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

History of this file

Take it

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

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

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

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

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

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

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

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

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

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 23–25why, one step back
23To do this we can create a function and hook it into the ``flask`` command
24that initializes the database. Let me show you the code first. Just add25this function below the `connect_db` function in `flaskr.py`::

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

line 26why, one step back
26

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 27–34why, one step back
27 @app.cli.command()
28 def initdb():29 """Initializes the database."""30 db = get_db()31 with app.open_resource('schema.sql', mode='r') as f:32 db.cursor().executescript(f.read())33 db.commit()34 print 'Initialized the database.'

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

line 35why, one step back
35

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 36–41why, one step back
36The ``app.cli.command()`` decorator registers a new command with the
37``flask`` script. When the command executes Flask will automatically38create a application context for us bound to the right application.39Within the function we can then access :attr:`flask.g` and other things as40we would expect. When the script ends, the application context tears down41and the database connection is released.

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 42–45why, one step back
42
43The :func:`~flask.Flask.open_resource` method of the application object44is a convenient helper function that will open a resource that the45application provides. This function opens a file from the resource

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

line 53why, one step back
53

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 54why, one step back
54Now it is possible to create a database with the ``flask`` script::

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

line 55why, one step back
55

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 56–57why, one step back
56 flask --app=flaskr initdb
57 Initialized the database.

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

lines 58–61why, one step back
58
59.. admonition:: Troubleshooting6061 If you get an exception later that a table cannot be found check that

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 62why, one step back
62 you did execute the `initdb` command and that your table names are

Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8

lines 63–64why, one step back
63 correct (singular vs. plural for example).
64

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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