QSqlQuery with provisioning and bindValue for Sqlite column name

void updateDB(const int id, const QString& column, const QVariant& value) const 
 //*****
 //all stuff on open DB etc. 
QSqlQuery query;
query.prepare("UPDATE table SET  :column = :value WHERE id = :id ");
query.bindValue(":column", QVariant(column));   
query.bindValue(":value", value);
query.bindValue(":id", id);
query.exec();

Does not work. Meanwhile, if I rewrite the request to

query.exec("UPDATE table SET " + column + " = " + value.toString() + " WHERE id = " + QString::number(id));

it works. It also works if I delete: column placeholder and write in the name of the query column on which I am testing this. It seems like I cannot use bindValue and placeholders for column names, at least with Sqlite. But I did not find any documentation about this.

So, is there no way to use bindValue and placeholders for column names, or am I missing something?

+5
source share
1 answer

The correct code is here:

query.prepare(QString("UPDATE table SET %1 = :value WHERE id = :id ").arg(column));
query.bindValue(":value", value);

You bind values, not field names.

P.S.: , . , - bindValues ​​ sqlite, db.

+11

All Articles