Qt 5 with SQLite: bindValue() results in "Parameter count mismatch" error

I just found out that my question is a duplicate of this one and that the corresponding answer is correct.

It turns out that query strings passed to the constructor of QSqlQuery are executed immediately, as per the documentation.

It isn't clear why such queries are working fine with Qt 4.8.5, or why they do work with Qt 5.3.1 when using positional parameters (using ? placeholders) while they fail with named parameters (using : placeholders).


I don't have the reputation to add a comment to the solution, so here, just to add yet another potential culprit: I was trying to bind a value to the column name and I got the Parameter count mismatch error, which seems to be the go-to error message no matter what the actual error is :)

It turns out this is simply an invalid approach to SQL, see QSqlQuery with prepare and bindValue for column name Sqlite.

So instead of

query.prepare("UPDATE connections SET (:columnName) = (:value) WHERE id = (:id)");
query.bindValue(":columnName", someColumnName);
query.bindValue(":value", someValue);
query.bindValue(":id", someId);

do this:

query.prepare(QString("UPDATE connections SET %1 = (:value) WHERE id = (:id)").arg(someColumnName));
query.bindValue(":value", someValue);
query.bindValue(":id", someId);
// etc.

This is not what the OP did but this thread is what came first during my search and it might save somebody else some time.