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.
  • N Offline
    N Offline
    Niagarer
    wrote on 6 May 2018, 09:34 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 May 2018, 09:38 last edited by
      #2

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

      N 1 Reply Last reply 6 May 2018, 09:40
      0
      • M mrjj
        6 May 2018, 09:38

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

        N Offline
        N Offline
        Niagarer
        wrote on 6 May 2018, 09:40 last edited by Niagarer 5 Jun 2018, 09:46
        #3

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

        M 1 Reply Last reply 6 May 2018, 09:49
        0
        • N Niagarer
          6 May 2018, 09:40

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 6 May 2018, 09:49 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.

          N 1 Reply Last reply 6 May 2018, 10:13
          0
          • M mrjj
            6 May 2018, 09:49

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

            N Offline
            N Offline
            Niagarer
            wrote on 6 May 2018, 10:13 last edited by Niagarer 5 Jun 2018, 10:13
            #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
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 6 May 2018, 10:16 last edited by
              #6

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

              N 1 Reply Last reply 6 May 2018, 10:17
              0
              • M mrjj
                6 May 2018, 10:16

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

                N Offline
                N Offline
                Niagarer
                wrote on 6 May 2018, 10:17 last edited by Niagarer 5 Jun 2018, 10:20
                #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

                M 1 Reply Last reply 6 May 2018, 10:20
                0
                • N Niagarer
                  6 May 2018, 10:17

                  @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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 6 May 2018, 10:20 last edited by mrjj 5 Jun 2018, 10:23
                  #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)+";");

                  N 1 Reply Last reply 6 May 2018, 10:42
                  2
                  • M mrjj
                    6 May 2018, 10:20

                    @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)+";");

                    N Offline
                    N Offline
                    Niagarer
                    wrote on 6 May 2018, 10:42 last edited by Niagarer 5 Jun 2018, 10:52
                    #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 6 May 2018, 10:42 last edited by Paul Colby 5 Jun 2018, 10:43
                      #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.

                      N 1 Reply Last reply 6 May 2018, 10:44
                      2
                      • Paul ColbyP Paul Colby
                        6 May 2018, 10:42

                        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.

                        N Offline
                        N Offline
                        Niagarer
                        wrote on 6 May 2018, 10:44 last edited by Niagarer 5 Jun 2018, 10:47
                        #11

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

                        1 Reply Last reply
                        0

                        1/11

                        6 May 2018, 09:34

                        • Login

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