Select from SQLite with Qt Select from SQLite with Qt sqlite sqlite

Select from SQLite with Qt


The isValid() method returns true if the query is positionned on a valid record, but after calling exec(), it isn't : you have to move to a valid record first, for example with query.first() or query.next().See Qt docs : http://doc.qt.io/archives/4.6/qsqlquery.html

The size() returning -1 doesn't mean there is no result : SQLite is one of the databases for which the size of the query is not directly available (look in the documentation for QSqlDriver::hasFeature()). You can check that rows are returned and find the size with a loop and query.next().

Depending on what you want to do with the result of your select, you could also use QSqlQueryModel instead of QSqlQuery.


In your "QSqlQuery query;" declaration, you have to specify the database connection, e.g. "QSqlQuery query(db)"


Given that your program reports that the query is invalid, have a look at the error message as follows:

QDebug() << query.lastError().text();

This should help you in debugging the problem.