CRUD
In every storage mechanism for data, there are four basic types of functions: Create, Read, Update, and Delete (CRUD). These allow all the basic ways of manipulating and viewing data needed for our web apps. To use these functions, we will use an object on the database named the session. Sessions will be explained later in the chapter, but for now, think of them as a storage location for all of our changes to the database.
Creating models
To create a new row in your database using our models, add the model to the session
and commit
objects. Adding an object to the session marks its changes for saving, and committing is when the session is saved to the database as follows:
>>> user = User(username='fake_name') >>> db.session.add(user) >>> db.session.commit()
It is simple to add a new row to our table.
Reading models
After we have added data to our database, data can be queried using Model.query
. For those who use SQLAlchemy, this is shorthand for db.session...