dbinit.rst
This file
Path
docs/tutorial/dbinit.rst
73 lines · 3.0 KiB
Last changed by
Update for python3 (#1973)SaturnRbefore adoption55bd39c
History of this fileTake it
The walk stopped after 200 commits: lines older than 41622c8 point at it.
Link article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 1–2why, one step back1.. _tutorial-dbinit:2Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 3why, one step back3Step 5: Creating The DatabaseLink article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 4–18why, one step back4=============================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 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 alsoMerge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 19why, one step back19requires that you provide the path to the database, which can introduceLink article about deploying on Azure/IISDavid Lordbefore adoption41622c8
line 20why, one step back20errors. It's a good idea to add a function that initializes the databaseMerge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 21why, one step back21for you, to the application.Link article about deploying on Azure/IISDavid Lordbefore adoption41622c8
line 22why, one step back22Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 23–26why, one step back23To do this, you can create a function and hook it into a :command:`flask`24command that initializes the database. For now just take a look at the25code segment below. A good place to add this function, and command, is26just below the `connect_db` function in :file:`flaskr.py`::Link article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 27–37why, one step back2728 def init_db():29 db = get_db()30 with app.open_resource('schema.sql', mode='r') as f:31 db.cursor().executescript(f.read())32 db.commit()3334 @app.cli.command('initdb')35 def initdb_command():36 """Initializes the database."""37 init_db()Update for python3 (#1973)SaturnRbefore adoption55bd39c
line 38why, one step back38 print('Initialized the database.')Link article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 39–41why, one step back3940The ``app.cli.command()`` decorator registers a new command with the41:command:`flask` script. When the command executes, Flask will automaticallyMerge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 42–44why, one step back42create an application context which is bound to the right application.43Within the function, you can then access :attr:`flask.g` and other things as44you might expect. When the script ends, the application context tears downLink article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 45–46why, one step back45and the database connection is released.46Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 47why, one step back47You will want to keep an actual function around that initializes the database,Link article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 48–53why, one step back48though, so that we can easily create databases in unit tests later on. (For49more information see :ref:`testing`.)5051The :func:`~flask.Flask.open_resource` method of the application object52is a convenient helper function that will open a resource that the53application provides. This function opens a file from the resourceMerge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 54–55why, one step back54location (the :file:`flaskr/flaskr` folder) and allows you to read from it.55It is used in this example to execute a script on the database connection.Link article about deploying on Azure/IISDavid Lordbefore adoption41622c8
line 56why, one step back56Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 57–58why, one step back57The connection object provided by SQLite can give you a cursor object.58On that cursor, there is a method to execute a complete script. Finally, youLink article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 59–63why, one step back59only have to commit the changes. SQLite3 and other transactional60databases will not commit unless you explicitly tell it to.6162Now, it is possible to create a database with the :command:`flask` script::63Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 64why, one step back64 flask initdbLink article about deploying on Azure/IISDavid Lordbefore adoption41622c8
lines 65–73why, one step back65 Initialized the database.6667.. admonition:: Troubleshooting6869 If you get an exception later on stating that a table cannot be found, check70 that you did execute the ``initdb`` command and that your table names are71 correct (singular vs. plural, for example).7273Continue with :ref:`tutorial-views`