dbinit.rst
This file
Path
docs/tutorial/dbinit.rst
69 lines · 2.8 KiB
Last changed by
Merge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8
History of this fileTake it
The walk stopped after 200 commits: lines older than 11c7b1d point at it.
Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption11c7b1d
lines 1–25why, one step back1.. _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` statementMerge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption7fc0ddb
lines 26–27why, one step back26first (`__future__` imports must be the very first import). Accordingly,27add the following lines to your existing imports in `flaskr.py`::Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption11c7b1d
lines 28–33why, one step back2829 from __future__ import with_statement30 from contextlib import closing3132Next we can create a function called `init_db` that initializes the33database. For this we can use the `connect_db` function we definedMerge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption7fc0ddb
line 34why, one step back34earlier. Just add that function below the `connect_db` function inMerge pull request #613 from untitaker/issue611Armin Ronacherbefore adoption5583fe8
line 35why, one step back35`flaskr.py`::Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption11c7b1d
lines 36–69why, one step back3637 def init_db():38 with closing(connect_db()) as db:39 with app.open_resource('schema.sql') as f:40 db.cursor().executescript(f.read())41 db.commit()4243The :func:`~contextlib.closing` helper function allows us to keep a44connection open for the duration of the `with` block. The45:func:`~flask.Flask.open_resource` method of the application object46supports that functionality out of the box, so it can be used in the47`with` block directly. This function opens a file from the resource48location (your `flaskr` folder) and allows you to read from it. We are49using this here to execute a script on the database connection.5051When we connect to a database we get a connection object (here called52`db`) that can give us a cursor. On that cursor there is a method to53execute a complete script. Finally we only have to commit the changes.54SQLite 3 and other transactional databases will not commit unless you55explicitly tell it to.5657Now it is possible to create a database by starting up a Python shell and58importing and calling that function::5960>>> from flaskr import init_db61>>> init_db()6263.. admonition:: Troubleshooting6465 If you get an exception later that a table cannot be found check that66 you did call the `init_db` function and that your table names are67 correct (singular vs. plural for example).6869Continue with :ref:`tutorial-dbcon`