QSqlQuery - clear bound values
-
Hello,
Let's say we have something like this
QSqlQuery qrySample;
And prepare the query this way
qrySample.prepare(sSomeParameterizedSQL);
And use it this way
qrySample.addBindValue(iSomeIntValue); qrySample.exec();
What's the correct way of reusing the already prepared query?
Like this??
qrySample.boundValues().clear(); qrySample.addBindValue(iSomeOTHERIntValue); qrySample.exec();
Thanks for your comments. =)
-
After exec() you can simply bind your new values.
-
After exec() you can simply bind your new values.
@Christian-Ehrlicher said in QSqlQuery - clear bound values:
After exec() you can simply bind your new values.
Just checked and exec() does not clear the bindings, which is useful to repeat queries .
But also, like you say, it's possible to just bind again.
I find this behavior a little annoying.
Thanks for your comment.
-
@Alvein said in QSqlQuery - clear bound values:
I find this behavior a little annoying.
In which way?
-
@Alvein said in QSqlQuery - clear bound values:
I find this behavior a little annoying.
In which way?
@Christian-Ehrlicher said in QSqlQuery - clear bound values:
@Alvein said in QSqlQuery - clear bound values:
I find this behavior a little annoying.
In which way?
The way that bindings are not reset but you can bind again as if they were.
If you bind two parameters and after the first exec() you bind only one...
qrySample.addBindValue(iSomeIntValue); qrySample.addBindValue(iOtherIntValue); qrySample.exec();
...the next exec() will succeed because a previous parameter (iOtherIntValue in this sample) was already in the second position.
qrySample.addBindValue(iYetAnotherIntValue); qrySample.exec();
That's not intuitive. Binding again should require some sort of resetting. But I'm not blaming this class because I don't know if every supported database type works the same way, and I don't have the time to check that myself.
BTW, the above sample is definitely a poor programming practice. It's just there to show what I'm talking about.
-
For example the windows odbc api behaves exactly the same. You have to take care what you're doing.