Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 73 lines (55 sloc) 3.038 kb
9d19b77 @mitsuhiko Added lazyloading pattern and explicit chapter links in tutorial.
authored
1 .. _tutorial-dbinit:
2
05161d3 @mitsuhiko Rewrote tutorial to use the g based appcontext object
authored
3 Step 4: Creating The Database
bcf347f @mitsuhiko New style for the documentation. Looks more like the website now.
authored
4 =============================
5
0d648fa Changed the wording of some sentences there were difficult to understand...
Reetta Vaahtoranta authored
6 As outlined earlier, Flaskr is a database powered application, and more
7 precisely, it is an application powered by a relational database system. Such
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
8 systems need a schema that tells them how to store that information.
9 Before starting the server for the first time, it's important to create
bcf347f @mitsuhiko New style for the documentation. Looks more like the website now.
authored
10 that schema.
11
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
12 Such a schema can be created by piping the ``schema.sql`` file into the
bcf347f @mitsuhiko New style for the documentation. Looks more like the website now.
authored
13 `sqlite3` command as follows::
14
15 sqlite3 /tmp/flaskr.db < schema.sql
16
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
17 The downside of this is that it requires the ``sqlite3`` command to be
18 installed, which is not necessarily the case on every system. This also
19 requires that we provide the path to the database, which can introduce
afd3c45 @mitsuhiko Rewrapped lines
authored
20 errors. It's a good idea to add a function that initializes the database
bcf347f @mitsuhiko New style for the documentation. Looks more like the website now.
authored
21 for you to the application.
22
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
23 To do this, we can create a function and hook it into the :command:`flask`
24 command that initializes the database. Let me show you the code first. Just
25 add this function below the `connect_db` function in :file:`flaskr.py`::
a3a5075 @mitsuhiko Updated tutorial to the flask script
authored
26
b9013ed @mitsuhiko Clarified how to test with click
authored
27 def init_db():
a3a5075 @mitsuhiko Updated tutorial to the flask script
authored
28 db = get_db()
29 with app.open_resource('schema.sql', mode='r') as f:
30 db.cursor().executescript(f.read())
31 db.commit()
b9013ed @mitsuhiko Clarified how to test with click
authored
32
33 @app.cli.command('initdb')
34 def initdb_command():
35 """Initializes the database."""
36 init_db()
a3a5075 @mitsuhiko Updated tutorial to the flask script
authored
37 print 'Initialized the database.'
38
39 The ``app.cli.command()`` decorator registers a new command with the
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
40 :command:`flask` script. When the command executes, Flask will automatically
a3a5075 @mitsuhiko Updated tutorial to the flask script
authored
41 create a application context for us bound to the right application.
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
42 Within the function, we can then access :attr:`flask.g` and other things as
a3a5075 @mitsuhiko Updated tutorial to the flask script
authored
43 we would expect. When the script ends, the application context tears down
44 and the database connection is released.
05161d3 @mitsuhiko Rewrote tutorial to use the g based appcontext object
authored
45
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
46 We want to keep an actual functions around that initializes the database,
47 though, so that we can easily create databases in unit tests later on. (For
b9013ed @mitsuhiko Clarified how to test with click
authored
48 more information see :ref:`testing`.)
49
05161d3 @mitsuhiko Rewrote tutorial to use the g based appcontext object
authored
50 The :func:`~flask.Flask.open_resource` method of the application object
51 is a convenient helper function that will open a resource that the
52 application provides. This function opens a file from the resource
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
53 location (your ``flaskr`` folder) and allows you to read from it. We are
bcf347f @mitsuhiko New style for the documentation. Looks more like the website now.
authored
54 using this here to execute a script on the database connection.
55
05161d3 @mitsuhiko Rewrote tutorial to use the g based appcontext object
authored
56 The connection object provided by SQLite can give us a cursor object.
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
57 On that cursor, there is a method to execute a complete script. Finally, we
58 only have to commit the changes. SQLite3 and other transactional
05161d3 @mitsuhiko Rewrote tutorial to use the g based appcontext object
authored
59 databases will not commit unless you explicitly tell it to.
bcf347f @mitsuhiko New style for the documentation. Looks more like the website now.
authored
60
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
61 Now, it is possible to create a database with the :command:`flask` script::
bcf347f @mitsuhiko New style for the documentation. Looks more like the website now.
authored
62
a3a5075 @mitsuhiko Updated tutorial to the flask script
authored
63 flask --app=flaskr initdb
64 Initialized the database.
1007761 @mitsuhiko Added troubleshooting section to the tutorial
authored
65
66 .. admonition:: Troubleshooting
67
23fc2e5 @iKevinY Grammatical fixes in Foreword and Tutorial
iKevinY authored
68 If you get an exception later on stating that a table cannot be found, check
69 that you did execute the ``initdb`` command and that your table names are
70 correct (singular vs. plural, for example).
9d19b77 @mitsuhiko Added lazyloading pattern and explicit chapter links in tutorial.
authored
71
05161d3 @mitsuhiko Rewrote tutorial to use the g based appcontext object
authored
72 Continue with :ref:`tutorial-views`
Something went wrong with that request. Please try again.