Important: Please read the Qt Code of Conduct -

[SOLVED] QSqlRecord Update

  • Hi everyone. I have a small database with four columns. I have one field that gets updated at a later date so when I run an initial query I can find the record ok. I can also grab the record and add the data required. The problem is it does not actually save it into the database. I tried commit() and that does not work so I believe I am missing something. Anyone see the problem here?

    QString queryString = "SELECT CommandSent, AckRvcd FROM CommandSequence";
    queryString.append(" WHERE CommandSent = '" + reverseCommandHash[returnedCommand] + "'");

             QSqlQuery query(commandDB);
                if(query.value(1).toString() == "")
                    QSqlRecord ackRecord = query.record();
                    ackRecord.setValue(1, "True");
                    qDebug() << "Field " << ackRecord.value(1);
                    /* Tried this but it adds a new row
                    query.prepare("INSERT INTO CommandSequence (AckRvcd)"
                                     "VALUES (:AckRvcd)");
                    //Have to create a second query
                    query.bindValue(":AckRvcd", "True");


  • Hi,

    your second approach will work if you add a new record.
    To update a record use:

    "UPDATE CommandSequence "
    "SET AckRvcd = :AckRvcd"
    "WHERE CommandSent = :cSent"
    query.bindValue(":CommandSent", "test");
    query.bindValue(":AckRvcd", "True");


  • Thanks and I did try that. The issue really is if I have more that one "test" records in the CommandSent it fills all of them with true, even if previous happened to be false. I just want to select the last one added to the database and not all of them.

  • If you initialise AckRvcd with NULL you may modify your UPDATE like that:

    UPDATE CommandSequence SET AckRvcd = :AckRvcd
    WHERE CommandSent = :cSent AND AckRvcd IS NULL;

    or use AckRvcd IS NOT FALSE...

  • Great thanks I will try that.

Log in to reply