I try to deal with SQLite database on Qt 4.5.3 on Linux. I've already created the databsae.
Then, I try to perform select on Qt:
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(filename); // Here is FULL path to the database. I've checked it twice :)
bool ok = db.open();
qDebug() << db.tables();
QSqlQuery query;
query.exec("select * from lessons");
qDebug() << query.size();
qDebug() << query.isSelect();
qDebug() << query.isValid();
But debug console says:
("lessons", "weeklessons", "weeks")
-1
true
false
Why it's select nothing? What I have doing wrong?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With