我正在使用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()返回无查询无法获取行

不知道为什么会这样,但真的想找出答案。

最佳答案

请使用prepare作为official example:

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

错误的原因是查询是在绑定(bind)到相应的占位符之前执行的。您可以看到constructor documentation的相关部分:

09-06 06:09