Skip to content
Myr

dbinit.rst

This file

Path

docs/tutorial/dbinit.rst

67 lines · 2.7 KiB

Last changed by

Not known.

History of this file

Take it

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

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 1–67why, one step back
1.. _tutorial-dbinit:
23Step 3: Creating The Database4=============================56Flaskr is a database powered application as outlined earlier, and more7precisely, an application powered by a relational database system. Such8systems need a schema that tells them how to store that information. So9before starting the server for the first time it's important to create10that schema.1112Such a schema can be created by piping the `schema.sql` file into the13`sqlite3` command as follows::1415 sqlite3 /tmp/flaskr.db < schema.sql1617The downside of this is that it requires the sqlite3 command to be18installed which is not necessarily the case on every system. Also one has19to provide the path to the database there which leaves some place for20errors. It's a good idea to add a function that initializes the database21for you to the application.2223If you want to do that, you first have to import the24:func:`contextlib.closing` function from the contextlib package. If you25want to use Python 2.5 it's also necessary to enable the `with` statement26first (`__future__` imports must be the very first import)::2728 from __future__ import with_statement29 from contextlib import closing3031Next we can create a function called `init_db` that initializes the32database. For this we can use the `connect_db` function we defined33earlier. Just add that function below the `connect_db` function::3435 def init_db():36 with closing(connect_db()) as db:37 with app.open_resource('schema.sql') as f:38 db.cursor().executescript(f.read())39 db.commit()4041The :func:`~contextlib.closing` helper function allows us to keep a42connection open for the duration of the `with` block. The43:func:`~flask.Flask.open_resource` method of the application object44supports that functionality out of the box, so it can be used in the45`with` block directly. This function opens a file from the resource46location (your `flaskr` folder) and allows you to read from it. We are47using this here to execute a script on the database connection.4849When we connect to a database we get a connection object (here called50`db`) that can give us a cursor. On that cursor there is a method to51execute a complete script. Finally we only have to commit the changes.52SQLite 3 and other transactional databases will not commit unless you53explicitly tell it to.5455Now it is possible to create a database by starting up a Python shell and56importing and calling that function::5758>>> from flaskr import init_db59>>> init_db()6061.. admonition:: Troubleshooting6263 If you get an exception later that a table cannot be found check that64 you did call the `init_db` function and that your table names are65 correct (singular vs. plural for example).6667Continue with :ref:`tutorial-dbcon`