Configuring the Database
Time to write some code. There’s some support code you’ll need to handle the database as well as the
User class. First, create a file
schema.sql that will contain the definition of the table to store the users.
This will make it possible to execute commands from the command prompt. You’ll also need a couple of imports from
get_db() function simply creates a database connection on the global object
g, if one does not already exist, and then returns it.
The rest of the code is to set up running the
init_db() function in response to CLI arguments. The last helper file contains the
User class, which is used by
flask_login to store user data in the database.
And one more static method to create a new user in the database, given values for the columns. The
User class just wraps the
sqlite3 API with two methods to get and create users. It’s kind of like a simple ORM.
Become a Member to join the conversation.