Reputation: 1354
I have a working Qt application that uses MySQL as a Database, but I decided to change the database to Sqlite in order to deploy on android. I created the same DB in Sqlite with the same data and tables structure. I changed the connection to the Sqlite successfully.
By testing the App (in Desktop) I found that some queries works fine with both Sqlite and MySQL, but in some other places the query doesn't return any data in Sqlite while working fine in MySQL.
Its not a query problem, because I changed the Query to a simple SELECT * FROM TABLE_NAME
and I still get the same problem.
Here is a simple code snippet
QSqlQuery qry;
qry.prepare("SELECT * FROM users;");
if( !qry.exec() ){
qDebug()<< qry.lastError().text().toLatin1();
qDebug()<< "data" ;
}
else if (qry.size() < 1) {
qDebug()<< "There is no users" << qry.size();
}
else {
qDebug()<< "It Works !!" << qry.size();
}
While using Sqlite I always get there is no users -1 But in MySQL it returns the right number of the rows in the table.
Any suggestion what the problem might be! Is it related to speed or something?
Upvotes: 0
Views: 294
Reputation: 6017
From Qt's documentation on QSqlQuery
:
Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.
SQLite is actually the one which doesn't provide this information. You can confirm it with:
qDebug() << qry.driver().hasFeature(QSqlDriver::QuerySize);
In case of SQLite you need to iterate through all rows from the results and count the results by yourself.
If you just need to count rows in users
table, then it's better to do:
qry.prepare("SELECT count(*) FROM users;");
This will always tell you number of rows as a single-cell result. No need for special features of database. This will work everywhere (unless some database doesn't support count(*)
function, then please correct me in comments).
Upvotes: 2