dbinit.rst
This file
Path
docs/tutorial/dbinit.rst
80 lines · 3.1 KiB
Last changed by
resolve merge conflictsKenneth Reitzbefore adoption31174fe
History of this fileTake it
1.. _tutorial-dbinit:23Step 5: 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.9Before starting the server for the first time, it's important to create10that schema.1112Such a schema could be created by piping the ``schema.sql`` file into the13``sqlite3`` command as follows::1415 sqlite3 /tmp/flaskr.db < schema.sql1617However, the downside of this is that it requires the ``sqlite3`` command18to be installed, which is not necessarily the case on every system. This19also requires that you provide the path to the database, which can introduce20errors.2122Instead of the ``sqlite3`` command above, it's a good idea to add a function23to our application that initializes the database for you. To do this, you24can create a function and hook it into a :command:`flask` command that25initializes the database.2627Take a look at the code segment below. A good place to add this function,28and command, is just below the ``connect_db`` function in :file:`flaskr.py`::2930 def init_db():31 db = get_db()3233 with app.open_resource('schema.sql', mode='r') as f:34 db.cursor().executescript(f.read())3536 db.commit()373839 @app.cli.command('initdb')40 def initdb_command():41 """Initializes the database."""4243 init_db()44 print('Initialized the database.')4546The ``app.cli.command()`` decorator registers a new command with the47:command:`flask` script. When the command executes, Flask will automatically48create an application context which is bound to the right application.49Within the function, you can then access :attr:`flask.g` and other things as50you might expect. When the script ends, the application context tears down51and the database connection is released.5253You will want to keep an actual function around that initializes the database,54though, so that we can easily create databases in unit tests later on. (For55more information see :ref:`testing`.)5657The :func:`~flask.Flask.open_resource` method of the application object58is a convenient helper function that will open a resource that the59application provides. This function opens a file from the resource60location (the :file:`flaskr/flaskr` folder) and allows you to read from it.61It is used in this example to execute a script on the database connection.6263The connection object provided by SQLite can give you a cursor object.64On that cursor, there is a method to execute a complete script. Finally, you65only have to commit the changes. SQLite3 and other transactional66databases will not commit unless you explicitly tell it to.6768Now, in a terminal, from the application root directory :file:`flaskr/` it is69possible to create a database with the :command:`flask` script::7071 flask initdb72 Initialized the database.7374.. admonition:: Troubleshooting7576 If you get an exception later on stating that a table cannot be found, check77 that you did execute the ``initdb`` command and that your table names are78 correct (singular vs. plural, for example).7980Continue with :ref:`tutorial-views`