Having used QSqlQueryModel in the last section, you might wonder whether this approach can be further generalized to just access tables directly and avoid having to write SQL queries altogether. You also might wonder if we can get around the read-only limitations of QSqlQueryModel. The answer to both questions is yes, thanks to QSqlTableModel and QSqlRelationalTableModels.
To see how these work, let's back up and start over with our application:
- Start with a fresh template copy, calling it coffee_list2.py. Add the import for QtSql and the database connection code from the first application. Now let's start building using table models. For simple situations where we want to create a model from a single database table, we can use QSqlTableModel:
self.reviews_model = qts.QSqlTableModel()
self.reviews_model.setTable('reviews&apos...