Solved QSqlQuery is not substituting bound values on exec upon second run
-
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
-
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.
-
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.
-
Did I miss where the variable rowsAffected was set in the code presented?
-
@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 aint QSqlQuery::numRowsAffected() const
, but you don't show any call to it?