dbinit.rst
This file
Path
docs/tutorial/dbinit.rst
72 lines · 3.0 KiB
Last changed by
Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
History of this fileTake it
The walk stopped after 200 commits: lines older than d1d835c point at it.
Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
lines 1–2why, one step back1.. _tutorial-dbinit:2Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 3why, one step back3Step 4: Creating The DatabaseAdded SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
lines 4–5why, one step back4=============================5Changed the wording of some sentences there were difficult to understand.Reetta Vaahtorantabefore adoption0d648fa
lines 6–7why, one step back6As outlined earlier, Flaskr is a database powered application, and more7precisely, it is an application powered by a relational database system. SuchGrammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
lines 8–9why, one step back8systems need a schema that tells them how to store that information.9Before starting the server for the first time, it's important to createAdded SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
lines 10–11why, one step back10that schema.11Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
line 12why, one step back12Such a schema can be created by piping the ``schema.sql`` file into theAdded SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
lines 13–16why, one step back13`sqlite3` command as follows::1415 sqlite3 /tmp/flaskr.db < schema.sql16Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
lines 17–19why, one step back17The 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 introduceRewrapped linesArmin Ronacherbefore adoptionafd3c45
line 20why, one step back20errors. It's a good idea to add a function that initializes the databaseAdded SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
lines 21–22why, one step back21for you to the application.22Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
lines 23–25why, one step back23To do this, we can create a function and hook it into the :command:`flask`24command that initializes the database. Let me show you the code first. Just25add this function below the `connect_db` function in :file:`flaskr.py`::Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
line 26why, one step back26Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed
line 27why, one step back27 def init_db():Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
lines 28–31why, one step back28 db = get_db()29 with app.open_resource('schema.sql', mode='r') as f:30 db.cursor().executescript(f.read())31 db.commit()Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed
lines 32–36why, one step back3233 @app.cli.command('initdb')34 def initdb_command():35 """Initializes the database."""36 init_db()Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
line 37why, one step back37 print 'Initialized the database.'Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
line 38why, one step back38Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
line 39why, one step back39The ``app.cli.command()`` decorator registers a new command with theGrammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
line 40why, one step back40:command:`flask` script. When the command executes, Flask will automaticallyUpdated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
line 41why, one step back41create a application context for us bound to the right application.Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
line 42why, one step back42Within the function, we can then access :attr:`flask.g` and other things asUpdated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
lines 43–44why, one step back43we would expect. When the script ends, the application context tears down44and the database connection is released.Clarified how to test with clickArmin Ronacherbefore adoptionb9013ed
line 45why, one step back45Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
lines 46–47why, one step back46We want to keep an actual functions around that initializes the database,47though, so that we can easily create databases in unit tests later on. (ForClarified how to test with clickArmin Ronacherbefore adoptionb9013ed
line 48why, one step back48more information see :ref:`testing`.)Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 49–52why, one step back4950The :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 resourceGrammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
line 53why, one step back53location (your ``flaskr`` folder) and allows you to read from it. We areAdded SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
lines 54–55why, one step back54using this here to execute a script on the database connection.55Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 56why, one step back56The connection object provided by SQLite can give us a cursor object.Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
lines 57–58why, one step back57On that cursor, there is a method to execute a complete script. Finally, we58only have to commit the changes. SQLite3 and other transactionalRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 59why, one step back59databases will not commit unless you explicitly tell it to.Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
line 60why, one step back60Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
line 61why, one step back61Now, it is possible to create a database with the :command:`flask` script::Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
line 62why, one step back62Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
lines 63–64why, one step back63 flask --app=flaskr initdb64 Initialized the database.Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
lines 65–67why, one step back6566.. admonition:: Troubleshooting67Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5
lines 68–70why, one step back68 If you get an exception later on stating that a table cannot be found, check69 that you did execute the ``initdb`` command and that your table names are70 correct (singular vs. plural, for example).Added SESSION_REFRESH_EACH_REQUEST config option.Armin Ronacherbefore adoptiond1d835c
line 71why, one step back71Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 72why, one step back72Continue with :ref:`tutorial-views`