我试过了
QSqlQuery query;
query.prepare("DELETE FROM names WHERE id_col = :ID OR id_parent = :ID");
query.bindValue(":ID", idVal);
query.exec();
假设 idVal 将被绑定(bind)两次,但执行此查询仅删除 id_parent = idVal 的行,id_col = idVal 的行保持未删除。所以只有第二次 idVal 被绑定(bind)到查询。
当我将它重写为
QSqlQuery query;
query.prepare("DELETE FROM names WHERE id_col = ? OR id_parent = ?");
query.bindValue(0, idVal);
query.bindValue(1, idVal);
query.exec();
一切都按预期进行。
这是一种在 QSqlQuery 中多次使用同一个命名占位符的方法吗?
最佳答案
来自QSqlQuery::bindValue() documentation :
Values cannot be bound to multiple locations in the query, eg:
INSERT INTO testtable (id, name, samename) VALUES (:id, :name, :name)
Binding to name will bind to the first :name, but not the second.
最后一句话似乎有点错误,因为它看起来像是绑定(bind)到第二个 :name,但不管怎样,这清楚地说明了 Qt 不支持您想要实现的目标。
您的选择是坚持您已有的解决方法,或者使用 Mahmoud Gamal 在您的问题的评论中提供的解决方案。
关于c++ - SqlQuery 一个命名占位符多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14277381/