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. Error: QSqlQuery::value: not positioned on a valid record
Forum Updated to NodeBB v4.3 + New Features

Error: QSqlQuery::value: not positioned on a valid record

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.8k Views 2 Watching
  • 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by
    #1

    Hi! This error is not unknown, I know, but I can't find a solution working for me.
    This is the code:

    query.exec("INSERT INTO Output (ID INTEGER, Number INTEGER)"
               "VALUES ("+ID+", "+Number+");");
    
    query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";");
    query.first();
    query.next();
    int ID = query.value("ID").toInt();  // here it prints:  QSqlQuery::value: not positioned on a valid record
    

    Any idea, what I missed?
    Thanks!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Maybe the query return no rows ? as in there is data that matches.

      NiagarerN 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Maybe the query return no rows ? as in there is data that matches.

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by Niagarer
        #3

        @mrjj Hi
        But how? I inserted exactely this row above. And there occcur no errors

        mrjjM 1 Reply Last reply
        0
        • NiagarerN Niagarer

          @mrjj Hi
          But how? I inserted exactely this row above. And there occcur no errors

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Niagarer
          ok so u have error checking code even if not shown ?

          You could use
          http://sqlitebrowser.org/
          to see what is going on.

          also the first() + next () seems a bit odd and might
          move it over the last record

          normally one do
          while (query.next() )
          ...

          but its not really possible to guess at :)
          you should just check data base and see if it even inserted.

          NiagarerN 1 Reply Last reply
          0
          • mrjjM mrjj

            @Niagarer
            ok so u have error checking code even if not shown ?

            You could use
            http://sqlitebrowser.org/
            to see what is going on.

            also the first() + next () seems a bit odd and might
            move it over the last record

            normally one do
            while (query.next() )
            ...

            but its not really possible to guess at :)
            you should just check data base and see if it even inserted.

            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by Niagarer
            #5

            @mrjj
            Well recently, the insert also failed, but the query told me that. Now it does not print anymore errors there.
            Ok, well the insert works kind of (at the first position in my script it works, at the second, 10 lines below, it doesn't, although it is actually the same...).
            But at least, I can see one filled table in the browser. Anyway, the following query.value() still fails.
            When I execute the "SELECT * FROM..."-command in the browser on the table, it works, I always get a row.
            Removing the query.first() unfortunately doesn't help.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Ok
              if select * works then maybe its the where part not working
              What database is it ?
              I assume sqllite ?

              NiagarerN 1 Reply Last reply
              0
              • mrjjM mrjj

                Ok
                if select * works then maybe its the where part not working
                What database is it ?
                I assume sqllite ?

                NiagarerN Offline
                NiagarerN Offline
                Niagarer
                wrote on last edited by Niagarer
                #7

                @mrjj Yep. Well the complete command works in the browser, so the where does not seem to be the problem itself
                0_1525602000682_Qt 34.png

                mrjjM 1 Reply Last reply
                0
                • NiagarerN Niagarer

                  @mrjj Yep. Well the complete command works in the browser, so the where does not seem to be the problem itself
                  0_1525602000682_Qt 34.png

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Niagarer
                  ok seems fine then.
                  The where syntax is not the usual one. but might also work. not sure.
                  Could you try with bindValue ? ( shows insert here but select is the same)

                    QSqlQuery query;
                    int ok = query.prepare(("INSERT INTO person (id, firstname, lastname) VALUES (:name, :first, :last)"));
                    query.bindValue(":id", 4);
                    query.bindValue(":first", "Lars junior");
                    query.bindValue(":last", "Gordon");
                    query.exec();
                  

                  also
                  query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";");

                  is Number an int ?
                  if yes, it should be
                  AND Number == "+QString::number(Number)+";");

                  NiagarerN 1 Reply Last reply
                  2
                  • mrjjM mrjj

                    @Niagarer
                    ok seems fine then.
                    The where syntax is not the usual one. but might also work. not sure.
                    Could you try with bindValue ? ( shows insert here but select is the same)

                      QSqlQuery query;
                      int ok = query.prepare(("INSERT INTO person (id, firstname, lastname) VALUES (:name, :first, :last)"));
                      query.bindValue(":id", 4);
                      query.bindValue(":first", "Lars junior");
                      query.bindValue(":last", "Gordon");
                      query.exec();
                    

                    also
                    query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";");

                    is Number an int ?
                    if yes, it should be
                    AND Number == "+QString::number(Number)+";");

                    NiagarerN Offline
                    NiagarerN Offline
                    Niagarer
                    wrote on last edited by Niagarer
                    #9

                    @mrjj
                    The number is already converted to a string.
                    Ok, I've done it

                    query.prepare(("INSERT INTO Output (ID, Number) VALUES (:id, :number)"));
                    query.bindValue(":id", ID);
                    query.bindValue(":number", i);
                    query.exec();
                    
                    query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";");
                    query.next();
                    int ID = query.value("ID").toInt();
                    

                    And I got the solution :)
                    There are two main problems with my version above.

                    1. Don't use query.first() and then query.next() like I did (damn, yes this was actually obvious sry...) The query.first() takes the first item and the query.next() is going into nowhere, because I already took the last item with query.first() -.- Use query.first() or query.next() in this case.
                    2. The actually interesting problem: Use query.prepare() and bindValue() instead of doing it directly by calling query.exec("INSERT INTO ..."). I have no idea why... but obviously the query has a problem with that...

                    by doing both of these things I got it working.
                    Thank you @mrjj @Paul-Colby !!

                    1 Reply Last reply
                    0
                    • Paul ColbyP Offline
                      Paul ColbyP Offline
                      Paul Colby
                      wrote on last edited by Paul Colby
                      #10

                      Hmm... I'm probably missing something (seems a little too obvious, but just in case)... isn't the problem:

                      query.first();
                      query.next();
                      

                      The first line fetches the first (and presumably only) records. The second line moves past that, where there are no more records?

                      Cheers.

                      Edit: Ah, seems you beat me to it :) Cheers.

                      NiagarerN 1 Reply Last reply
                      2
                      • Paul ColbyP Paul Colby

                        Hmm... I'm probably missing something (seems a little too obvious, but just in case)... isn't the problem:

                        query.first();
                        query.next();
                        

                        The first line fetches the first (and presumably only) records. The second line moves past that, where there are no more records?

                        Cheers.

                        Edit: Ah, seems you beat me to it :) Cheers.

                        NiagarerN Offline
                        NiagarerN Offline
                        Niagarer
                        wrote on last edited by Niagarer
                        #11

                        @Paul-Colby
                        yes, also just realized that. Doesn't make sense what I did, sorry :D

                        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