PDA

View Full Version : What does QSqlError(-1, "", "") mean?



KeineAhnung
23rd April 2014, 10:54
Hi again,

I keep getting the QSqlError(-1, "", "") return from lastError() after I try to select a row from a SQLite table. I checked the query buy using it directly in the SQLite table via the FireFox plugin. The connection to the database is also okay. The code I am using to get the line I have been using over and over and it always worked as intended. I just cannot figure out what the difference is here. Does anyone know what this error message means?
Here is the code that is not working:


if(conOpen()){ // here I open the connection to the db and get an output connected, so the connection is okay
QSqlQuery* query=new QSqlQuery(myDB);
query->prepare("SELECT * FROM chars WHERE type='steel' AND class='V2A'");
if(query->exec() && query->size() > 0){ // if I don't use the size() the next lines provides an error telling me that there is no value(1)
qDebug() << query->value(1).toString();
}else{
qDebug() << query->lastError(); // -> here I get the error QSqlError(-1, "", "")
}
conClose();
}else{
ui->log->append("No connection");
}

anda_skoa
23rd April 2014, 11:26
This is most likely the default constructed QSqlError instance, i.e. no error.

Cheers,
_

Lesiok
23rd April 2014, 12:04
This is "no error" because query->exec() == true but query->size() == 0. Perhaps query->size() == 0 is an error in the logic of your program but it is not a SQL error.
P.S.
Using construction
query.exec("SELECT * FROM ....");
query.value(1);is bad because no database does not guarantee the order of the columns in such queries.

KeineAhnung
23rd April 2014, 21:26
Hi!

Thanks for the hints. If I had configured my profile right I would have gotten a info that there were replies and would not have wasted the entire day...

Your are absolutely right. QSqlError(-1, "", "") is no error. I added an query->next() and then I had all my data.

ChrisW67
23rd April 2014, 21:38
Only some database drivers support returning the result set size. See QSqlDriver::hasFeature()