[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); query.prepare(queryString); query.exec(); while(query.next()) { 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.previous(); query.prepare("INSERT INTO CommandSequence (AckRvcd)" "VALUES (:AckRvcd)"); //Have to create a second query query.bindValue(":AckRvcd", "True"); query.exec();*/ }
}
@ -
Hi,
your second approach will work if you add a new record.
To update a record use:@
query.prepare(
"UPDATE CommandSequence "
"SET AckRvcd = :AckRvcd"
"WHERE CommandSent = :cSent"
);
query.bindValue(":CommandSent", "test");
query.bindValue(":AckRvcd", "True");
query.exec();@
-