13

我正在使用 SQLite,插入到表中。跟随

QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(?)"));
testQuery.bindValue(0, someQStringObg);
testQuery.exec();

有效,但是

QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(:val)"));
testQuery.bindValue(":val", someQStringObg);
testQuery.exec();

不。testQuery.lastError().text() 返回无查询无法获取行

不知道为什么会这样,但真的很想知道。

4

1 回答 1

15

请使用 prepare 作为官方示例

QSqlQuery testQuery;
testQuery.prepare("INSERT INTO test(testcol) VALUES(:val)");
testQuery.bindValue(":val", someQStringObj);
testQuery.exec();

错误的原因是在绑定到相应的占位符之前执行了查询。您可以查看构造函数文档的相关部分:

如果查询不是空字符串,它将被执行。

于 2013-12-26T14:30:04.543 回答