gpt4 book ai didi

c++ - SqlQuery 一个命名占位符多次

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:19 31 4
gpt4 key购买 nike

我试过了

  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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com