Skip to content
Myr

dbinit.rst

This file

Path

docs/tutorial/dbinit.rst

73 lines · 3.0 KiB

Last changed by

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

History of this file

Take it

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

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

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

line 12why, one step back
12Such a schema can be created by piping the ``schema.sql`` file into the

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

lines 13–16why, one step back
13`sqlite3` command as follows::
1415 sqlite3 /tmp/flaskr.db < schema.sql16

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

lines 17–18why, one step back
17The downside of this is that it requires the ``sqlite3`` command to be
18installed, which is not necessarily the case on every system. This also

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 21why, one step back
21for you, to the application.

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

line 22why, one step back
22

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 23–26why, one step back
23To do this, you can create a function and hook it into a :command:`flask`
24command that initializes the database. For now just take a look at the25code segment below. A good place to add this function, and command, is26just below the `connect_db` function in :file:`flaskr.py`::

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

line 27why, one step back
27

Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed

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

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

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

Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

line 39why, one step back
39

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

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

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

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 45why, one step back
45and the database connection is released.

Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed

line 46why, one step back
46

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

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

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

Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed

line 49why, one step back
49more information see :ref:`testing`.)

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

line 56why, one step back
56

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

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

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

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

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

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

line 63why, one step back
63

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 64why, one step back
64 flask initdb

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 65why, one step back
65 Initialized the database.

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

lines 66–68why, one step back
66
67.. admonition:: Troubleshooting68

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

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

Add "." to sys.path by defaultArmin Ronacherbefore adoptionbacdd07

lines 72–73why, one step back
72
73Continue with :ref:`tutorial-views`