PDA

View Full Version : Error executing SELECT query with QSQLITE



garfield85
24th May 2009, 20:00
Hi all,

I am experiencing trouble executing a sqlite SELECT query using the Qt library(4.5.0). Finally i broke it down to this small example app, which fails after executing the SELECT query without reporting an error, but query.isValid() returns false. Initializing the database and inserting values is fine and the sqlite command line utility is able to show the created entries, only Qt fails.



#include <QtGui/QApplication>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");
db.open();
QSqlQuery query = db.exec("CREATE TABLE vars "
"(id integer primary key,"
"name VARCHAR( 250 ),"
"value VARCHAR( 250 ))");
QSqlError err = db.lastError();
if(!err.text().size() > 1)
QMessageBox::information(0,"Error creating Database",err.text());

query = db.exec("INSERT INTO `vars` ( `name` , `value` ) VALUES ( 'testvar', 'testval');");
err = db.lastError();
if(!err.text().size() > 1)
QMessageBox::information(0,"Error inserting to Database",err.text());

query = db.exec("SELECT * from vars;");
err = db.lastError();
if(!err.text().size() > 1)
QMessageBox::information(0,"Error inserting to Database",err.text());
if(!query.isValid()) {
QMessageBox::information(0,"SELECT query not valid!",err.text());
}
if(!query.size()>0)
QMessageBox::information(0,"SELECT result","At least one result read!");
else
QMessageBox::information(0,"SELECT failed","Select failed!");

return a.exec();
}


Can someone tell my what I am missing?

Thanks,
garfield85

wysota
24th May 2009, 20:11
What if you remove the semicolon from the query?

garfield85
24th May 2009, 21:38
i just tried it without semicolons and got the same error...am i missing something generally? is spent several hours on that issue today and don't know what's wrong :(

wysota
24th May 2009, 21:45
Try putting the name of the table in quotes like you do for other queries.

garfield85
24th May 2009, 22:39
Hi!

I also tried that but also with no effect. Did anybody get this to work? They only use the model/view thing in the Qt documentation but thats not what I need...

thanks wysota for your suggestions! I'll wait some time if someone perhaps knows the trick or will have to use the sqlite library directly. But a simple select should not be an uncommon problem :-)

garfield85

wysota
25th May 2009, 00:05
The code is more or less correct. The problem is that you didn't read the docs carefully enough ;) QSqlQuery::size() may return -1 if the number of rows in the result can't be determined. Just iterate the result with QSqlQuery::next().

garfield85
25th May 2009, 18:05
I was pretty sure i read this part of the documentation and also tried it with query.next(), but I gave it a shot once again and it worked!

Thanks a lot for your patience, wysota!

Greetings,
garfield85