dbinit.rst
This file
Path
docs/tutorial/dbinit.rst
72 lines · 2.9 KiB
Last changed by
Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
History of this fileTake it
1.. _tutorial-dbinit:23Step 4: Creating The Database4=============================56As outlined earlier, Flaskr is a database powered application, and more7precisely, it is 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. This also19requires that we provide the path to the database which can introduce20errors. It's a good idea to add a function that initializes the database21for you to the application.2223To do this we can create a function and hook it into the :command:`flask` command24that initializes the database. Let me show you the code first. Just add25this function below the `connect_db` function in :file:`flaskr.py`::2627 def init_db():28 db = get_db()29 with app.open_resource('schema.sql', mode='r') as f:30 db.cursor().executescript(f.read())31 db.commit()3233 @app.cli.command('initdb')34 def initdb_command():35 """Initializes the database."""36 init_db()37 print 'Initialized the database.'3839The ``app.cli.command()`` decorator registers a new command with the40:command:`flask` script. When the command executes Flask will automatically41create a application context for us bound to the right application.42Within the function we can then access :attr:`flask.g` and other things as43we would expect. When the script ends, the application context tears down44and the database connection is released.4546We want to keep an actual functions around that initializes the database47though so that we can easily create databases in unittests later. (For48more information see :ref:`testing`.)4950The :func:`~flask.Flask.open_resource` method of the application object51is a convenient helper function that will open a resource that the52application provides. This function opens a file from the resource53location (your `flaskr` folder) and allows you to read from it. We are54using this here to execute a script on the database connection.5556The connection object provided by SQLite can give us a cursor object.57On that cursor there is a method to execute a complete script. Finally we58only have to commit the changes. SQLite 3 and other transactional59databases will not commit unless you explicitly tell it to.6061Now it is possible to create a database with the :command:`flask` script::6263 flask --app=flaskr initdb64 Initialized the database.6566.. admonition:: Troubleshooting6768 If you get an exception later that a table cannot be found check that69 you did execute the `initdb` command and that your table names are70 correct (singular vs. plural for example).7172Continue with :ref:`tutorial-views`