JPFrancoia
JPFrancoia

Reputation: 5629

Set query as a model and limit it further

In my program, I use a sqlite database, with a table "papers". I have several QTableviews displaying the data contained in papers. One model per view. I initialize my model like this:

model.setTable("papers")

However, in each QTableView, I want to display only a sub-set of the data. This subset is determined by a sql query. Ex:

query = QtSql.QSqlQuery()
query.prepare("SELECT id FROM papers WHERE new=1")
query.exec_()
model.setQuery(query)

But then, if I want to perform ADDITIONAL queries on the subset (not on the entire model), it starts to become really complicated. So I wonder if there is a way to either:

Ex:

After I selected all the items with new=1, I want all of them with liked=1.

Upvotes: 2

Views: 291

Answers (1)

transistor
transistor

Reputation: 669

Use a QSqlTableview for each view. Add a model to each, just as you did. Add additional filtering to any table by adding for example

model.setfilter(new=1);

This filter results in a where clause in the underlying select.

Upvotes: 1

Related Questions