2010-05-26 20:49:01 +08:00
.. _tutorial-dbinit:
2013-09-01 03:32:41 +08:00
Step 4: Creating The Database
2010-04-18 19:15:00 +08:00
=============================
2013-11-09 21:13:14 +08:00
As outlined earlier, Flaskr is a database powered application, and more
precisely, it is an application powered by a relational database system. Such
2010-04-18 19:15:00 +08:00
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
that schema.
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
2013-11-09 21:13:14 +08:00
installed which is not necessarily the case on every system. This also require 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
2010-04-18 19:15:00 +08:00
for you to the application.
2013-09-01 03:32:41 +08:00
To do this we can create a function called `init_db` that initializes the
2013-11-09 21:13:14 +08:00
database. Let me show you the code first. Just add this function below
2013-09-01 03:32:41 +08:00
the `connect_db` function in `flaskr.py` ::
2010-05-30 02:06:12 +08:00
2010-04-18 19:15:00 +08:00
def init_db():
2013-09-04 11:36:10 +08:00
with app.app_context():
2013-09-01 03:32:41 +08:00
db = get_db()
2013-05-26 01:13:48 +08:00
with app.open_resource('schema.sql', mode='r') as f:
2010-04-18 19:15:00 +08:00
db.cursor().executescript(f.read())
db.commit()
2013-09-01 03:32:41 +08:00
So what's happening here? Remember how we learned last chapter that the
application context is created every time a request comes in? Here we
don't have a request yet, so we need to create the application context by
hand. Without an application context the :data: `~flask.g` object does not
know yet to which application it becomes as there could be more than one!
The `` with app.app_context() `` statement establishes the application
context for us. In the body of the with statement the :flask: `~flask.g`
object will be associated with `` app `` . At the end of the with statement
the association is released and all teardown functions are executed. This
means that our database connection is disconnected after the commit.
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
2010-04-18 19:15:00 +08:00
location (your `flaskr` folder) and allows you to read from it. We are
using this here to execute a script on the database connection.
2013-09-01 03:32:41 +08:00
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
databases will not commit unless you explicitly tell it to.
2010-04-18 19:15:00 +08:00
Now it is possible to create a database by starting up a Python shell and
importing and calling that function::
>>> from flaskr import init_db
>>> init_db()
2010-04-19 00:40:52 +08:00
.. admonition :: Troubleshooting
If you get an exception later that a table cannot be found check that
you did call the `init_db` function and that your table names are
correct (singular vs. plural for example).
2010-05-26 20:49:01 +08:00
2013-09-01 03:32:41 +08:00
Continue with :ref: `tutorial-views`