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

标签 c++ sql qt binding

我试过了

  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/

相关文章:

SQL如何使作业运行?

c++ - 设置每个样式表的 QStatusBar 最小高度

c++ - 将 const 基引用强制转换为派生类

c++ - 如何正确初始化 std::array 的 vector ?

c++ - 在 CView 而不是 CMainFrame 中创建停靠面板

c++ - 初始化列表没有合适的默认构造函数可用错误

sql - 在 SQL Server 2008 中用逗号分割字符串

c++ - C++中二维float数组的序列化与反序列化

sql - 查找自动递增字段中插入的值

c++ - 如何使 dumpObjectInfo 打印调试信息?