Changed the wording of some sentences there were difficult to understand.
This commit is contained in:
parent
d4f8634289
commit
0d648fa468
4 changed files with 13 additions and 20 deletions
|
|
@ -3,8 +3,8 @@
|
||||||
Step 4: Creating The Database
|
Step 4: Creating The Database
|
||||||
=============================
|
=============================
|
||||||
|
|
||||||
Flaskr is a database powered application as outlined earlier, and more
|
As outlined earlier, Flaskr is a database powered application, and more
|
||||||
precisely, an application powered by a relational database system. Such
|
precisely, it is an application powered by a relational database system. Such
|
||||||
systems need a schema that tells them how to store that information. So
|
systems need a schema that tells them how to store that information. So
|
||||||
before starting the server for the first time it's important to create
|
before starting the server for the first time it's important to create
|
||||||
that schema.
|
that schema.
|
||||||
|
|
@ -15,13 +15,11 @@ Such a schema can be created by piping the `schema.sql` file into the
|
||||||
sqlite3 /tmp/flaskr.db < schema.sql
|
sqlite3 /tmp/flaskr.db < schema.sql
|
||||||
|
|
||||||
The downside of this is that it requires the sqlite3 command to be
|
The downside of this is that it requires the sqlite3 command to be
|
||||||
installed which is not necessarily the case on every system. Also one has
|
installed 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 database
|
||||||
to provide the path to the database there which leaves some place for
|
|
||||||
errors. It's a good idea to add a function that initializes the database
|
|
||||||
for you to the application.
|
for you to the application.
|
||||||
|
|
||||||
To do this we can create a function called `init_db` that initializes the
|
To do this we can create a function called `init_db` that initializes the
|
||||||
database. Let me show you the code first. Just add that function below
|
database. Let me show you the code first. Just add this function below
|
||||||
the `connect_db` function in `flaskr.py`::
|
the `connect_db` function in `flaskr.py`::
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,8 @@ application::
|
||||||
/templates
|
/templates
|
||||||
|
|
||||||
The `flaskr` folder is not a python package, but just something where we
|
The `flaskr` folder is not a python package, but just something where we
|
||||||
drop our files. Directly into this folder we will then put our database
|
drop our files. We will then put our database schema as well as main module
|
||||||
schema as well as main module in the following steps. The files inside
|
into this folder. It is done in the following way. The files inside
|
||||||
the `static` folder are available to users of the application via `HTTP`.
|
the `static` folder are available to users of the application via `HTTP`.
|
||||||
This is the place where css and javascript files go. Inside the
|
This is the place where css and javascript files go. Inside the
|
||||||
`templates` folder Flask will look for `Jinja2`_ templates. The
|
`templates` folder Flask will look for `Jinja2`_ templates. The
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,8 @@
|
||||||
Step 1: Database Schema
|
Step 1: Database Schema
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
First we want to create the database schema. For this application only a
|
First we want to create the database schema. Only a single table is needed
|
||||||
single table is needed and we only want to support SQLite so that is quite
|
for 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:
|
||||||
easy. Just put the following contents into a file named `schema.sql` in
|
|
||||||
the just created `flaskr` folder:
|
|
||||||
|
|
||||||
.. sourcecode:: sql
|
.. sourcecode:: sql
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,10 @@ Step 2: Application Setup Code
|
||||||
==============================
|
==============================
|
||||||
|
|
||||||
Now that we have the schema in place we can create the application module.
|
Now that we have the schema in place we can create the application module.
|
||||||
Let's call it `flaskr.py` inside the `flaskr` folder. For starters we
|
Let's call it flaskr.py. We will place this file inside the flask folder.
|
||||||
will add the imports and create the application object. For small
|
We will begin by adding the imports we need and by adding the config section.
|
||||||
applications it's a possibility to drop the configuration directly into
|
For small applications, it is possible to drop the configuration directly into
|
||||||
the module which we will be doing here. However a cleaner solution would
|
the 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 the
|
||||||
be to create a separate `.ini` or `.py` file and load that or import the
|
|
||||||
values from there.
|
values from there.
|
||||||
|
|
||||||
First we add the imports in `flaskr.py`::
|
First we add the imports in `flaskr.py`::
|
||||||
|
|
@ -66,9 +65,7 @@ debug flag enables or disables the interactive debugger. *Never leave
|
||||||
debug mode activated in a production system*, because it will allow users to
|
debug mode activated in a production system*, because it will allow users to
|
||||||
execute code on the server!
|
execute code on the server!
|
||||||
|
|
||||||
We also add a method to easily connect to the database specified. That
|
We 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 a
|
||||||
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 a
|
|
||||||
simple database connection through SQLite and then tell it to use the
|
simple database connection through SQLite and then tell it to use the
|
||||||
:class:`sqlite3.Row` object to represent rows. This allows us to treat
|
:class:`sqlite3.Row` object to represent rows. This allows us to treat
|
||||||
the rows as if they were dictionaries instead of tuples.
|
the rows as if they were dictionaries instead of tuples.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue