forked from orbit-oss/flask
Grammatical fixes in Foreword and Tutorial
This commit is contained in:
parent
475d7076f0
commit
23fc2e56a8
12 changed files with 98 additions and 96 deletions
|
|
@ -5,24 +5,24 @@ Step 4: Creating The Database
|
|||
|
||||
As outlined earlier, Flaskr is a database powered application, and more
|
||||
precisely, it is an application powered by a relational database system. Such
|
||||
systems need a schema that tells them how to store that information. So
|
||||
before starting the server for the first time it's important to create
|
||||
systems need a schema that tells them how to store that information.
|
||||
Before starting the server for the first time, it's important to create
|
||||
that schema.
|
||||
|
||||
Such a schema can be created by piping the `schema.sql` file into the
|
||||
Such a schema can be created by piping the ``schema.sql`` file into the
|
||||
`sqlite3` command as follows::
|
||||
|
||||
sqlite3 /tmp/flaskr.db < schema.sql
|
||||
|
||||
The downside of this is that it requires the sqlite3 command to be
|
||||
installed which is not necessarily the case on every system. This also
|
||||
requires that we provide the path to the database which can introduce
|
||||
The downside of this is that it requires the ``sqlite3`` command to be
|
||||
installed, which is not necessarily the case on every system. This also
|
||||
requires that we provide the path to the database, which can introduce
|
||||
errors. It's a good idea to add a function that initializes the database
|
||||
for you to the application.
|
||||
|
||||
To do this we can create a function and hook it into the :command:`flask` command
|
||||
that initializes the database. Let me show you the code first. Just add
|
||||
this function below the `connect_db` function in :file:`flaskr.py`::
|
||||
To do this, we can create a function and hook it into the :command:`flask`
|
||||
command that initializes the database. Let me show you the code first. Just
|
||||
add this function below the `connect_db` function in :file:`flaskr.py`::
|
||||
|
||||
def init_db():
|
||||
db = get_db()
|
||||
|
|
@ -37,36 +37,36 @@ this function below the `connect_db` function in :file:`flaskr.py`::
|
|||
print 'Initialized the database.'
|
||||
|
||||
The ``app.cli.command()`` decorator registers a new command with the
|
||||
:command:`flask` script. When the command executes Flask will automatically
|
||||
:command:`flask` script. When the command executes, Flask will automatically
|
||||
create a application context for us bound to the right application.
|
||||
Within the function we can then access :attr:`flask.g` and other things as
|
||||
Within the function, we can then access :attr:`flask.g` and other things as
|
||||
we would expect. When the script ends, the application context tears down
|
||||
and the database connection is released.
|
||||
|
||||
We want to keep an actual functions around that initializes the database
|
||||
though so that we can easily create databases in unittests later. (For
|
||||
We want to keep an actual functions around that initializes the database,
|
||||
though, so that we can easily create databases in unit tests later on. (For
|
||||
more information see :ref:`testing`.)
|
||||
|
||||
The :func:`~flask.Flask.open_resource` method of the application object
|
||||
is a convenient helper function that will open a resource that the
|
||||
application provides. This function opens a file from the resource
|
||||
location (your `flaskr` folder) and allows you to read from it. We are
|
||||
location (your ``flaskr`` folder) and allows you to read from it. We are
|
||||
using this here to execute a script on the database connection.
|
||||
|
||||
The connection object provided by SQLite can give us a cursor object.
|
||||
On that cursor there is a method to execute a complete script. Finally we
|
||||
only have to commit the changes. SQLite 3 and other transactional
|
||||
On that cursor, there is a method to execute a complete script. Finally, we
|
||||
only have to commit the changes. SQLite3 and other transactional
|
||||
databases will not commit unless you explicitly tell it to.
|
||||
|
||||
Now it is possible to create a database with the :command:`flask` script::
|
||||
Now, it is possible to create a database with the :command:`flask` script::
|
||||
|
||||
flask --app=flaskr initdb
|
||||
Initialized the database.
|
||||
|
||||
.. admonition:: Troubleshooting
|
||||
|
||||
If you get an exception later that a table cannot be found check that
|
||||
you did execute the `initdb` command and that your table names are
|
||||
correct (singular vs. plural for example).
|
||||
If you get an exception later on stating that a table cannot be found, check
|
||||
that you did execute the ``initdb`` command and that your table names are
|
||||
correct (singular vs. plural, for example).
|
||||
|
||||
Continue with :ref:`tutorial-views`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue