PDA

View Full Version : QSQLITE database can't exec queries



mcwar
11th January 2010, 14:30
Why this code is not working?


db = QSqlDatabase::addDatabase("QSQLITE"); //I defined the "db" at header.
db.setDatabaseName("../database");
db.open();
QSqlQuery query(db);
query.exec("SELECT id FROM lesson");
QMessageBox::about(0,"",query.value(0).toString());


code is running without any error but value which in message box is null. There is no result at query value.

wysota
11th January 2010, 17:35
Probably your relative path to the database is incorrect. See if QFile::exists("../database") returns true (check that before you open the database) and if that's indeed the database file you want.

mcwar
12th January 2010, 12:57
I checked that. I am not a newbie. db.open returned true, db.exists returned true also db.tables return my tables but it can't exec queries.

wysota
12th January 2010, 13:06
Ah, now I see the problem. You forgot to position the cursor on the first row of the result. Run query.first() before calling query.value().

BTW. db.open() and db.exist() will always return true in case of SQLite (provided you have sufficient access rights to access the database file) so that's not a proof of anything.

mcwar
12th January 2010, 14:16
Oh thanks, it is running.

tangential
12th January 2010, 23:47
Of note, query.next() also functions as query.first() if it's the first navigation call in the program flow.