Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    [SOLVED] QSqlRecord Update

    General and Desktop
    2
    5
    1477
    Loading More Posts
    • 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
      Gundi last edited by

      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 Reply Quote 0
      • C
        clochydd last edited by

        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 Reply Quote 0
        • G
          Gundi last edited by

          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 Reply Quote 0
          • C
            clochydd last edited by

            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 Reply Quote 0
            • G
              Gundi last edited by

              Great thanks I will try that.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post