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. [SOLVED] QSqlRecord Update
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QSqlRecord Update

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.9k Views 1 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.
  • G Offline
    G Offline
    Gundi
    wrote on last edited by
    #1

    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(&#41;;*/
    
                }
    

    }
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

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

      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gundi
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gundi
            wrote on last edited by
            #5

            Great thanks I will try that.

            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