Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]QSqlQuery::value: not positioned on a valid record

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 30.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bobby
    wrote on last edited by
    #1

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

    @

    "Linux is not user-friendly." It is user-friendly. It is not ignorant-friendly and idiot-friendly.
    ---Source unknown

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexisdm
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bobby
          wrote on last edited by
          #4

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

          "Linux is not user-friendly." It is user-friendly. It is not ignorant-friendly and idiot-friendly.
          ---Source unknown

          1 Reply Last reply
          2
          • H Offline
            H Offline
            Harshi
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mduzoylum
              wrote on last edited by
              #6

              Hi @bobby,

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved