Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]QSqlQuery::value: not positioned on a valid record



  • 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());
    
    }
    

    @



  • 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.



  • 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 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!



  • 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().



  • Hi @bobby,

    Afer sql.exec(), you have to add sql.next() to get position of database record.
    Have a nice day.


Log in to reply