在c++中使用prepare()和bindvalue()陷入困境Qt
我写了一个基于Qt助手的SQL查询,它说你可以使用prepare()
方法而不是exec()
然后你可以通过两种方法的帮助:bindvalue()
和 addbindvalue()
这是我的问题的代码片段:
Query->prepare("SELECT ID , Row , Col FROM sometable WHERE Row = :row AND Col = :col");
Query->bindValue(":row" , __Row);
Query->bindValue(":col" ,__Col);
Query->exec();
qDebug("%s" , Query->executedQuery().toStdString().c_str());
输出:
从某个表中选择 ID、行、列,其中行 = ?和列 = ?
我还使用了另一种建议的方式:
Query->prepare("SELECT ID , Row , Col FROM sometable WHERE Row = :row AND Col = :col");
Query->addBindValue(0 , __Row);
Query->addBindValue(1 ,__Col);
Query->exec();
qDebug("%s" , Query->executedQuery().toStdString().c_str());
输出:
从某个表中选择 ID、行、列,其中行 = ?和列 = ?
但是当我正常使用 exec()
时,它工作得很好,并且会替换相应的值而不是“?”。
有什么解释吗?或者我应该使用普通的 exec() ?
I've written a SQL query based on Qt assistant and it says that you can use the prepare()
method instead of exec()
then you can pass your parameter by the help of two methods called :bindvalue()
and addbindvalue()
Here is an snippet code of my problem :
Query->prepare("SELECT ID , Row , Col FROM sometable WHERE Row = :row AND Col = :col");
Query->bindValue(":row" , __Row);
Query->bindValue(":col" ,__Col);
Query->exec();
qDebug("%s" , Query->executedQuery().toStdString().c_str());
output :
SELECT ID , Row , Col FROM sometable WHERE Row = ? AND Col = ?
and also I've used another suggested way :
Query->prepare("SELECT ID , Row , Col FROM sometable WHERE Row = :row AND Col = :col");
Query->addBindValue(0 , __Row);
Query->addBindValue(1 ,__Col);
Query->exec();
qDebug("%s" , Query->executedQuery().toStdString().c_str());
output :
SELECT ID , Row , Col FROM sometable WHERE Row = ? AND Col = ?
but when I use exec()
normally it works perfectly and will replace the corresponding values instead of "?".
is there any explanation about that? or should I use the ordinary exec()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
exec() 调用失败吗?
因为您所看到的可能没问题,因为根据您使用的 SQL 服务器,绑定可以由服务器(例如 Oracle)完成。
根据 Qt 文档,executeQuery:“在大多数情况下,此函数返回与 lastQuery() 相同的字符串。如果在不支持它的 DBMS 上执行带有占位符的准备好的查询,则该查询的准备被模仿”。因此,我想,如果服务器支持绑定值,则不会模拟准备工作,因此您只会看到查询,而占位符不会被实际值替换。
Is the exec() call failing ?
Because it may just be ok what you're seeing, as ,depending on which sql server you're using, the binding could be done by the server ( e.g. Oracle ).
According to Qt docs, executedQuery: "In most cases this function returns the same string as lastQuery(). If a prepared query with placeholders is executed on a DBMS that does not support it, the preparation of this query is emulated". So, I guess, if the server supports binding values the preparation won't be emulated so you'd just see the query without the placeholders being replaced by real values.
这只是一个猜测,但来自 http://qt.nokia.com/doc/4.6 /qsqlquery.html 我读了以下内容:
您的情况下连接是否打开?
This is just a guess but from http://qt.nokia.com/doc/4.6/qsqlquery.html I read the following:
Is the connection open in your case?
如果您想了解如何使用准备好的语句构造查询,您可以尝试此操作:
You can try this if you want to how your query constructed with prepared statements: