Skip to content
Myr

dbinit.rst

This file

Path

docs/tutorial/dbinit.rst

72 lines · 2.9 KiB

Last changed by

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

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-dbinit:
2

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

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

line 18why, one step back
18installed which is not necessarily the case on every system. This also

Merge pull request #1097 from slater/patch-2Kenneth Reitzbefore adoption6f990f6

line 19why, one step back
19requires that we provide the path to the database which can introduce

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

line 20why, one step back
20errors. It's a good idea to add a function that initializes the database

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

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

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 23why, one step back
23To do this we can create a function and hook it into the :command:`flask` command

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 24why, one step back
24that initializes the database. Let me show you the code first. Just add

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 26why, one step back
26

Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed

line 27why, one step back
27 def init_db():

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 28–31why, one step back
28 db = get_db()
29 with app.open_resource('schema.sql', mode='r') as f:30 db.cursor().executescript(f.read())31 db.commit()

Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed

lines 32–36why, one step back
32
33 @app.cli.command('initdb')34 def initdb_command():35 """Initializes the database."""36 init_db()

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 37why, one step back
37 print 'Initialized the database.'

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 38why, one step back
38

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 39why, one step back
39The ``app.cli.command()`` decorator registers a new command with the

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 40why, one step back
40:command:`flask` script. When the command executes Flask will automatically

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 41–44why, one step back
41create a application context for us bound to the right application.
42Within the function we can then access :attr:`flask.g` and other things as43we would expect. When the script ends, the application context tears down44and the database connection is released.

Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed

lines 45–48why, one step back
45
46We want to keep an actual functions around that initializes the database47though so that we can easily create databases in unittests later. (For48more information see :ref:`testing`.)

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 49–52why, one step back
49
50The :func:`~flask.Flask.open_resource` method of the application object51is a convenient helper function that will open a resource that the52application provides. This function opens a file from the resource

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 60why, one step back
60

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

line 62why, one step back
62

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

lines 65–68why, one step back
65
66.. admonition:: Troubleshooting6768 If you get an exception later that a table cannot be found check that

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

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

Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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