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. QSqlQuery is not substituting bound values on exec upon second run
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery is not substituting bound values on exec upon second run

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 237 Views 2 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by ocgltd
    #1

    I am performing queries against a MySQL database as shown below. The first time the code runs exec is succesfull and rowsAffected is 1. The second time I run the code exec is successfull, but rowsAffected is 0.

    I was expecting it to be 1. I realize upon second run the update doesn't do anything since the values are already set to the new values. Does that mean rowsAffected will only reflect rows that have changed? Shouldn't it be rows which matched the select?

    QSqlQuery* query = new QSqlQuery(m_db)
            query->prepare(QString("UPDATE companies SET "
                                   "NAME=:name, "
                                   "ISUSER=:isuser, "
                                   "ISVAR=:isvar, "
                                   "ISOEM=:isoem, "
                                   "CONTACT=:contact, "
                                   "EMAIL=:email, "
                                   "COMMENTS=:comments "
                                   "WHERE ID=:id "
                                   "LIMIT 1"));
            query->bindValue(":name",rowData.name);
            query->bindValue(":isuser",rowData.isEndUser);
            query->bindValue(":isvar",rowData.isVAR);
            query->bindValue(":isoem",rowData.isOEM);
            query->bindValue(":contact",rowData.contact);
            query->bindValue(":email",rowData.email);
            query->bindValue(":comments",rowData.comments);
            query->bindValue(":id",id);
            bool queryOk = query->exec();
            if (queryOk) {
                qDebug() << query->executedQuery();
                qDebug() << query->lastQuery();
                qDebug() << query->lastError().text();
                qDebug() << rowsAffected;
    

    There must be something different/wrong in the code above causing the output below:

    "UPDATE companies SET NAME=:name, ISUSER=:isuser, ISVAR=:isvar, ISOEM=:iSOEM, CONTACT=:contact, EMAIL=:email, COMMENTS=:comments WHERE ID=:id LIMIT 1"
    "UPDATE companies SET NAME=:name, ISUSER=:isuser, ISVAR=:isvar, ISOEM=:iSOEM, CONTACT=:contact, EMAIL=:email, COMMENTS=:comments WHERE ID=:id LIMIT 1"
    ""
    0
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Based on the documentation of the numRowsAffected:

      Returns the number of rows affected by the result's SQL statement.
      

      Your second call does not change anything and thus the 0 value seems accurate to me.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        Did I miss where the variable rowsAffected was set in the code presented?

        JonBJ 1 Reply Last reply
        2
        • C ChrisW67

          Did I miss where the variable rowsAffected was set in the code presented?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @ChrisW67
          It isn't, and I wondered about that, but these days one doesn't dare to ask when OPs state it is "obvious" what it is and/or did not paste their actual code but you should guess what it really is.....

          @ocgltd
          qDebug() << rowsAffected;
          Is this your actual code? rowsAffected is what --- an undeclared local variable, an unset member variable? There is a int QSqlQuery::numRowsAffected() const, but you don't show any call to it?

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved