Skip to content
Myr

Rewrapped lines

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 0d648fa · Browse files · 3 files+20 added−12 removed
This commit

State

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

Diff stats

3 files+20 added−12 removed

  • other:+20 added−12 removed

3 files+20 added−12 removed

modified:docs/tutorial/dbinit.rst+3 added-1 removed
13 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. 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 database18installed which is not necessarily the case on every system. This also19require that we provide the path to the database which can introduce20errors. It's a good idea to add a function that initializes the database21for you to the application.2223To do this we can create a function called `init_db` that initializes the24database. Let me show you the code first. Just add this function below
45 unmodified lines
modified:docs/tutorial/schema.rst+4 added-2 removed
1 unmodified line
23Step 1: Database Schema4=======================56First 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:6First we want to create the database schema. Only a single table is needed7for this application and we only want to support SQLite so creating the8database schema is quite easy. Just put the following contents into a file9named `schema.sql` in the just created `flaskr` folder:1011.. sourcecode:: sql1213 drop table if exists entries;
12 unmodified lines
modified:docs/tutorial/setup.rst+13 added-9 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. 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.7Let'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 config9section. For small applications, it is possible to drop the configuration10directly into the module, and this is what we will be doing here. However11a cleaner solution would be to create a separate `.ini` or `.py` file and12load that or import the values from there.1314First we add the imports in `flaskr.py`::1516 # all the imports
48 unmodified lines
65debug flag enables or disables the interactive debugger. *Never leave66debug mode activated in a production system*, because it will allow users to67execute code on the server!6868We 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.69We will also add a method that allows for easily connecting to the70specified database. This can be used to open a connection on request and71also from the interactive Python shell or a script. This will come in72handy later. We create a simple database connection through SQLite and73then tell it to use the :class:`sqlite3.Row` object to represent rows.74This allows us to treat the rows as if they were dictionaries instead of75tuples.7677::7879 def connect_db():
30 unmodified lines