Important: Please read the Qt Code of Conduct -

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

  • 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+";");
    int ID = query.value("ID").toInt();  // here it prints:  QSqlQuery::value: not positioned on a valid record

    Any idea, what I missed?

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

    You could use
    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 ( )

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

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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


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

  • @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);

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

    1. Don't use query.first() and then like I did (damn, yes this was actually obvious sry...) The query.first() takes the first item and the is going into nowhere, because I already took the last item with query.first() -.- Use query.first() or 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 !!

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


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


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

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

Log in to reply