Skip to content
Myr

Changed the wording of some sentences there were difficult to understand.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Reetta Vaahtoranta · committed by Reetta Vaahtoranta · parent d4f8634 · Browse files · 4 files+13 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+13 added−20 removed

  • other:+13 added−20 removed

4 files+13 added−20 removed

modified:docs/tutorial/dbinit.rst+4 added-6 removed
1 unmodified line
23Step 4: Creating The Database4=============================56Flaskr is a database powered application as outlined earlier, and more7precisely, an application powered by a relational database system. Such6As 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.11
2 unmodified lines
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 database18installed which is not necessarily the case on every system. This also require that we provide the path to the database which can introduce errors. It's a good idea to add a function that initializes the database19for you to the application.2021To do this we can create a function called `init_db` that initializes the24database. Let me show you the code first. Just add that function below22database. Let me show you the code first. Just add this function below23the `connect_db` function in `flaskr.py`::2425 def init_db():26 with app.app_context():
41 unmodified lines
modified:docs/tutorial/folders.rst+2 added-2 removed
9 unmodified lines
10 /static11 /templates1213The `flaskr` folder is not a python package, but just something where we14drop our files. Directly into this folder we will then put our database15schema as well as main module in the following steps. The files inside14drop our files. We will then put our database schema as well as main module15into this folder. It is done in the following way. The files inside16the `static` folder are available to users of the application via `HTTP`.17This is the place where css and javascript files go. Inside the18`templates` folder Flask will look for `Jinja2`_ templates. The19templates you create later in the tutorial will go in this directory.
4 unmodified lines
modified:docs/tutorial/schema.rst+2 added-4 removed
1 unmodified line
23Step 1: Database Schema4=======================56First we want to create the database schema. For this application only a7single table is needed and we only want to support SQLite so that is quite8easy. Just put the following contents into a file named `schema.sql` in9the just created `flaskr` folder:6First we want to create the database schema. Only a single table is needed 7for this application and we only want to support SQLite so creating the database schema is quite easy. Just put the following contents into a file named `schema.sql` in the just created `flaskr` folder:89.. sourcecode:: sql1011 drop table if exists entries;
12 unmodified lines
modified:docs/tutorial/setup.rst+5 added-8 removed
2 unmodified lines
3Step 2: Application Setup Code4==============================56Now that we have the schema in place we can create the application module.7Let's call it `flaskr.py` inside the `flaskr` folder. For starters we8will add the imports and create the application object. For small9applications it's a possibility to drop the configuration directly into10the module which we will be doing here. However a cleaner solution would11be to create a separate `.ini` or `.py` file and load that or import the7Let's call it flaskr.py. We will place this file inside the flask folder. 8We will begin by adding the imports we need and by adding the config section.9For small applications, it is possible to drop the configuration directly into 10the module, and this is what we will be doing here. However a cleaner solution would be to create a separate `.ini` or `.py` file and load that or import the11values from there.1213First we add the imports in `flaskr.py`::14
49 unmodified lines
64debug flag enables or disables the interactive debugger. *Never leave65debug mode activated in a production system*, because it will allow users to66execute code on the server!6769We also add a method to easily connect to the database specified. That70can be used to open a connection on request and also from the interactive71Python shell or a script. This will come in handy later. We create a68We will also add a method that allows for easily connecting to the specified database. This can be used to open a connection on request and also from the interactive Python shell or a script. This will come in handy later. We create a69simple database connection through SQLite and then tell it to use the70:class:`sqlite3.Row` object to represent rows. This allows us to treat71the rows as if they were dictionaries instead of tuples.72
33 unmodified lines