QtSql:绑定不会更改 SQLite 的查询
QtSql: binding doesn't change query with SQLite
我有以下代码应该将值绑定到 prepare 语句:
QSqlQuery query(db);
query.setForwardOnly(true);
query.prepare("SELECT Entry.* FROM Entry WHERE body LIKE ?;");
query.addBindValue(QVariant("%" + name + "%"));
query.exec();
tDebug("%s", query.executedQuery().toUtf8().data());
例如,如果 name
是 "thing"
,那么查询应该执行语句 SELECT Entry.* FROM Entry WHERE body LIKE "%thing%"
,但它执行 SELECT Entry.* FROM Entry WHERE body LIKE ?
,几乎就像绑定值是忽略。命名占位符也有同样的问题。
我在等效示例上尝试了此操作,并适当地测试了 "prepare" 和 "exec" 的结果,这两个 return 都是布尔值。我测试了在 exec with:
之后这些值是有界的
QList<QVariant> list = query.boundValues().values();
for (int i = 0; i < list.size(); ++i)
qDebug() << i << ": " << list.at(i).toString();
我用
测试了我得到了预期的结果
while (query.next())
qDebug()<<"result = "<<query.value(0);
确实,executedQuery
不包含有界值,但 Qt 中的注释对此有些含糊:
"If a prepared query with placeholders is executed on a DBMS that does not support it, the preparation of this query is emulated.The placeholders in the original query are replaced with their bound values to form a new query. This function returns the modified query. It is mostly useful for debugging purposes."
所以我假设对于 postgresql(我拥有的)和 SQLite,executedQuery
会 return 原始的占位符而不是有界值。
我有以下代码应该将值绑定到 prepare 语句:
QSqlQuery query(db);
query.setForwardOnly(true);
query.prepare("SELECT Entry.* FROM Entry WHERE body LIKE ?;");
query.addBindValue(QVariant("%" + name + "%"));
query.exec();
tDebug("%s", query.executedQuery().toUtf8().data());
例如,如果 name
是 "thing"
,那么查询应该执行语句 SELECT Entry.* FROM Entry WHERE body LIKE "%thing%"
,但它执行 SELECT Entry.* FROM Entry WHERE body LIKE ?
,几乎就像绑定值是忽略。命名占位符也有同样的问题。
我在等效示例上尝试了此操作,并适当地测试了 "prepare" 和 "exec" 的结果,这两个 return 都是布尔值。我测试了在 exec with:
之后这些值是有界的 QList<QVariant> list = query.boundValues().values();
for (int i = 0; i < list.size(); ++i)
qDebug() << i << ": " << list.at(i).toString();
我用
测试了我得到了预期的结果 while (query.next())
qDebug()<<"result = "<<query.value(0);
确实,executedQuery
不包含有界值,但 Qt 中的注释对此有些含糊:
"If a prepared query with placeholders is executed on a DBMS that does not support it, the preparation of this query is emulated.The placeholders in the original query are replaced with their bound values to form a new query. This function returns the modified query. It is mostly useful for debugging purposes."
所以我假设对于 postgresql(我拥有的)和 SQLite,executedQuery
会 return 原始的占位符而不是有界值。