Application Database
Regarding the Database and the connection.¶
For this example the Flask tutorial use the SQLite, since it is already integrated with python. As it is mentioned in the tutorial, the first thing we need to do when working with databases is to establish a connection, any operation or query to the DB is done through this connections, this connections must be close once the operation is finished.
flaskr/db.py
There are couple objects that still i don't fully understand but that make the development easier, or with better practice ( according with documentation), this are g
and current_app
, here some description ( and links) of this code snippet
-
g
1: it is an special object that is unique for each request, use to store data that might be user accessed by multiple function during the request. In this case the connection is stored and reused instead of creating a new one, if the get_db is use a second time at in the same request. -
current_app
2: Another special object, it points to the Flask application handling request, if we develop like this example we will be using the application factory, thus, we wont have an application object we writing the rest of the code, "theget_db()
will be call when the application is create and is handling a request, so current_app can be used." -
sqlite3.connect()
establish a connection to the file pointed at by theDATABASE
configuration key. at the beginning the file wont exist we need to initialize the database (I will explain it bellow) -
sqlite.Row
tells the connection to return rows that behave likedicts
. This allows accessing the columns by name.
Creating the tables¶
In this case the initial table will be store in a file .sql
flaskr/schema.sql
- Create the functions to run the SQL statements, this will be done on flask/db.py
- Register with the application, we need to let the application know that there is a database, create the initializer ( will include some CLI commands) flaskr/db.py, and later the logic to import it to the application factory flaskr/init.py.
Function to run the SQL commands¶
code to add: flaskr/db.py
so the code until on flaskr/db.py will be:
Register the application¶
Now the functions close_db
and init_db_command
are defined but they are not register to be use by the application, in other words, in order to use the functions we need to register them with the instance of the application, although, in this case we are using application factory, so, technically the applications doesn't exist yet, or the instance is not available, so in this case we will need a function that make the registration for us and later we will import that function on the factory.
We create a function on flaskr/db.py later import that function on flask/init.py in the factory function
create_app()
flaskr/db.py
1.app.teardown_appcontext()
this function is executed after returning the response, during the clean up process, and basically allow me to run a function in that moment, in that case close_db()
2. app.cli.add_command()
add a new command that can be call with the flask command.
so the flaskr/db.py will be
Now we need to call the function init_app()
from the factory
flaskr/init.py
Initialize the database¶
We create the Flask commands, we register those commands and import it to the factory, so now we can initialize the database using flask
on the Command (CMD) or terminal
There now on the instance folder we will have a file flask.sqlite
-
g
is a namespace object that can store data during an application context, this is a proxy ↩ -
current_app
A proxy to the application handling the current request. This is useful to access the application without needing to import it, or if it can’t be imported, such as when using the application factory pattern or in blueprints and extensions. ↩