Skip to content
Myr

dbinit.rst

This file

Path

docs/tutorial/dbinit.rst

80 lines · 3.1 KiB

Last changed by

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

History of this file

Take it

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

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

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

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

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

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 4–7why, one step back
4=============================
56As outlined earlier, Flaskr is a database powered application, and more7precisely, it is an application powered by a relational database system. Such

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

lines 8–9why, one step back
8systems need a schema that tells them how to store that information.
9Before starting the server for the first time, it's important to create

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 10–11why, one step back
10that schema.
11

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 12–13why, one step back
12Such a schema could be created by piping the ``schema.sql`` file into the
13``sqlite3`` command as follows::

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 14–16why, one step back
14
15 sqlite3 /tmp/flaskr.db < schema.sql16

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 17–20why, one step back
17However, the downside of this is that it requires the ``sqlite3`` command
18to be installed, which is not necessarily the case on every system. This19also requires that you provide the path to the database, which can introduce20errors.

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

line 21why, one step back
21

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 22–25why, one step back
22Instead of the ``sqlite3`` command above, it's a good idea to add a function
23to our application that initializes the database for you. To do this, you24can create a function and hook it into a :command:`flask` command that25initializes the database.

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

line 26why, one step back
26

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 27–29why, one step back
27Take a look at the code segment below. A good place to add this function,
28and command, is just below the ``connect_db`` function in :file:`flaskr.py`::29

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 30–31why, one step back
30 def init_db():
31 db = get_db()

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

line 32why, one step back
32

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 33–34why, one step back
33 with app.open_resource('schema.sql', mode='r') as f:
34 db.cursor().executescript(f.read())

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

line 35why, one step back
35

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 36–37why, one step back
36 db.commit()
37

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

line 38why, one step back
38

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 39–41why, one step back
39 @app.cli.command('initdb')
40 def initdb_command():41 """Initializes the database."""

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

line 42why, one step back
42

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

line 43why, one step back
43 init_db()

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

line 44why, one step back
44 print('Initialized the database.')

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 45–46why, one step back
45
46The ``app.cli.command()`` decorator registers a new command with the

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

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

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 48–50why, one step back
48create an application context which is bound to the right application.
49Within the function, you can then access :attr:`flask.g` and other things as50you might expect. When the script ends, the application context tears down

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 51–52why, one step back
51and the database connection is released.
52

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

line 53why, one step back
53You will want to keep an actual function around that initializes the database,

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 54why, one step back
54though, so that we can easily create databases in unit tests later on. (For

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 55–59why, one step back
55more information see :ref:`testing`.)
5657The :func:`~flask.Flask.open_resource` method of the application object58is a convenient helper function that will open a resource that the59application provides. This function opens a file from the resource

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 60–61why, one step back
60location (the :file:`flaskr/flaskr` folder) and allows you to read from it.
61It is used in this example to execute a script on the database connection.

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

line 62why, one step back
62

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 63–64why, one step back
63The connection object provided by SQLite can give you a cursor object.
64On that cursor, there is a method to execute a complete script. Finally, you

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 65why, one step back
65only have to commit the changes. SQLite3 and other transactional

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 66–67why, one step back
66databases will not commit unless you explicitly tell it to.
67

Merge branch 'master' into reload_templates_run_debugDavid Lordbefore adoption9e39c50

lines 68–69why, one step back
68Now, in a terminal, from the application root directory :file:`flaskr/` it is
69possible to create a database with the :command:`flask` script::

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

line 70why, one step back
70

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 71why, one step back
71 flask initdb

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 72–75why, one step back
72 Initialized the database.
7374.. admonition:: Troubleshooting75

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

lines 76–78why, one step back
76 If you get an exception later on stating that a table cannot be found, check
77 that you did execute the ``initdb`` command and that your table names are78 correct (singular vs. plural, for example).

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306

lines 79–80why, one step back
79
80Continue with :ref:`tutorial-views`