1

I have what I think is a very simple case, but I don't understand why it fails.

Database used is sqlite3, linux platform. Qt 5.3.

I have this table: CREATE TABLE db_info (name TEXT, value TEXT);

Containing this data (output from sqlite3 client):

version|1
created|2015-01-16 11:06:12

Yes, it's very simple.

The purpose is to check if the DB is created with another version of the application. So I compare the version in the DB with a compiled constant in the code.

Thus, during startup, I do this (trimmed for brevity):

_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", "mydb"));
_db->setDatabaseName(dbFile);
_db->open();

QSqlQuery query(*_db);
query.prepare("SELECT value FROM db_info WHERE name = 'version' AND value = :version");
query.bindValue(":version", DB_VERSION);
query.exec();
if(query.first())
  // same version
else
  // some other version

Not very complicated stuff... Now to the specific problem:

  • _db->open() returns true
  • query.prepare() returns true
  • query.isSelect() returns true
  • query.exec() returns true

After exec():

  • query.isActive() returns true
  • query.size() returns -1
  • query.first() or query.next() returns false

What could be the problem? According to the dox (http://doc.qt.io/qt-5/qsqlquery.html#first), first() returns false if the query is not active or it's not a select, so that can't be it. But I guess, since size() returns -1 there definitely is something amiss somewhere...

I've tried adding some calls to lastError() but it's always empty. Both from query and from _db. It there some other means of getting some mesages from the database, or driver or some such?

I've also tried removing the WHERE clause. No difference.

Running the exact same query in the sqlite3 client works fine.

pythonator
  • 384
  • 2
  • 12
  • Note that SQLite does not report the size of the query, so `size()` returning -1 is expected. – pajaja Jan 17 '15 at 17:22
  • Is the value of DB_VERSION what you expect it should be? – G B Jan 17 '15 at 18:20
  • Hmm... after trying different things, adding a lot of tracing and checks, it suddenly works. And still works after removing aforementioned traces and checks... :-| I don't know what caused it, and probably never will. This question may be removed. – pythonator Jan 17 '15 at 20:46
  • @GB: yep, it has never changed. It's just a `static const int DB_VERSION = 1;` at the top of the same file. I kind of suspect that the boolean algebra got the better of me somewhere, and not a problem with the usage of Qt... – pythonator Jan 17 '15 at 20:49
  • @pajaja: What do you mean? It does/should return the number of rows returned by a `SELECT` query. "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.". So I'm guessing the second reason, i.e. unknown. – pythonator Jan 22 '15 at 18:31
  • 1
    Yes, SQLite does not support reporting information about query sizes. Take a look at this http://stackoverflow.com/questions/5373184/qtsql-sqlite-and-support-for-size-function – pajaja Jan 22 '15 at 18:52
  • Ah! That confirms it. I didn't think to look at the sqlite API directly. I kind of assumed it supported it. Thanks for the link. – pythonator Jan 23 '15 at 14:54

0 Answers0