[SOLVED]QSqlQuery::value: not positioned on a valid record
-
wrote on 8 Aug 2011, 22:44 last edited by
Hello All,
After a few hours of reviewing the developer network I've got to ask: Why am I getting this error? I see a lot of people recommend making sure to call @query.next()@ before @query.value(0).toString()@ but I've called that before.
I've already connected to the db, here I am explicitly calling it to list the result of the query in a text area.Code is as follows:
@
QSqlDatabase db = QSqlDatabase::database("default_db_connection");
QSqlQuery query("SELECT table_name FROM information_schema.tables WHERE table_schema ='public';", db);while(query.next()); { if(query.isActive()) { QMessageBox::information(0,"Good Query", "Good Query. It\'s active"); } else { QMessageBox::warning(0, "Bad Query", "Bad Query, It\'s inactive"); } ui->textEdit->append(query.value(0).toString()); }
@
-
wrote on 8 Aug 2011, 23:02 last edited by
check the output of
@
qDebug() << query.lastError();
@I suspect an SQL syntax error, as you should write:
@
QSqlQuery query("SELECT table_name FROM information_schema.tables WHERE table_schema ='public'", db);
@Note the missing backslashes and leave out the semikolon at the end.
-
wrote on 9 Aug 2011, 00:46 last edited by
Remove the semi-colon after the while(query.next()) which makes the loop skip all valid records if there was any.
You should test isActive() only once outside the loop (I don't think there is a way to discriminate between an error and the last record once the query has successfully started) and show the error if that test fails as Volker said :
@if(query.isActive()) {
QMessageBox::information(0,"Good Query", "Good Query. It's active");
while(query.next())
{
ui->textEdit->append(query.value(0).toString());
}
} else {
QMessageBox::warning(0, "Bad Query",
QString("Bad Query, It's inactive: %1").arg(query.lastError().text());
}@ -
wrote on 9 Aug 2011, 16:47 last edited by
[quote author="alexisdm" date="1312850811"]Remove the semi-colon after the while(query.next()) which makes the loop skip all valid records if there was any.
You should test isActive() only once outside the loop (I don't think there is a way to discriminate between an error and the last record once the query has successfully started) and show the error if that test fails as Volker said :
@if(query.isActive()) {
QMessageBox::information(0,"Good Query", "Good Query. It's active");
while(query.next())
{
ui->textEdit->append(query.value(0).toString());
}
} else {
QMessageBox::warning(0, "Bad Query",
QString("Bad Query, It's inactive: %1").arg(query.lastError().text());
}@
[/quote]Awesome! This plus Volker's
[quote author="Volker" date="1312844579"]check the output of
@
qDebug() << query.lastError();
@I suspect an SQL syntax error, as you should write:
@
QSqlQuery query("SELECT table_name FROM information_schema.tables WHERE table_schema ='public'", db);
@Note the missing backslashes and leave out the semikolon at the end.[/quote]
Both made it work!
You guys rock. Thank you for taking time to answer!
-
wrote on 18 Mar 2014, 13:18 last edited by
You should call query.first() before you can access returned data. additionally if your query returns more than one row, you should iterate via query.next().