首页 文章

Qt QSqlQuery bindValue适用于?但不是:占位符

提问于
浏览
8

我正在使用SQLite,插入表中 . Folowwing

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()返回 No query Unable to fetch row

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

1 回答

  • 9

    请使用prepare作为official example

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

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

    如果查询不是空字符串,则将执行该字符串 .

相关问题