微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

qt sqlite3 delete Parameter count mismatch

在项目研发中,发现在利用QT操作sqlite3数据库的时候,在自己封装的工具类里面,进行delete操作的时候,数据库报错,总是说参数不匹配,但是发现自己传入的sql语句,以及bindValue都是对的。

无奈之下,通过在overstackflow查找答案,发现问题。

必须按照官网给的例子操作顺序来执行。

先prepare,再bindvalue,再exec执行。我的实现就是没有按照这个顺序,所以出现这个错误

问题回答原网站:https://stackoverflow.com/questions/20786003/qt-qsqlquery-bindvalue-works-with-but-not-with-placeholders

7down Votefavorite

I'm working with sqlite,doing insert into table. Folowwing

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

works,but

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

don't. testQuery.lastError().text() returnsNo query Unable to fetch row

Have no clue why things are that way,but really want to find out.

Which sql type and version are you using? Which Qt version? Which OS,which version? Have you tried explicit .prepate() call to see the return value? Also,it is strange that you mix the "?" and ":" approaches,though that should not matter.–lpappDec 26 '13 at 14:11

Try to print out the last query with this:qDebug () << query->lastQuery()just to make sure. What does that print out? Also,Could you please check if the table exists properly before the insertion of the second case? You Could use a command line client for double checking this.–lpappDec 26 '13 at 14:20

@LaszloPapp with prepare everything works! Thanks!–user3136871Dec 26 '13 at 14:29

OK,great. Submitted an answer for better readability.–lpappDec 26 '13 at 14:30

add a comment

1 Answer

activeoldestvotes

up Vote7down Voteaccepted

Please use prepare as theofficial example:

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

The reason for the error is that the query was executed before binding to the corresponding placeholder. You can see the relevant part of theconstructor documentation:

If query is not an empty string,it will be executed.

shareimprove this answer

原文地址:https://www.jb51.cc/sqlite/198105.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐