使用绑定调用服务器存储过程 [英] Calling server stored procedure using binding

查看:115
本文介绍了使用绑定调用服务器存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Qt调用MySQL服务器存储过程.该过程返回我要检索的多行数据.

I am trying to call a MySQL server stored procedure using Qt. The procedure returns multiple rows of data I want to retrieve.

我尝试的第一个代码片段效果很好:

The first snippet I tried works fine:

QSqlQuery query("CALL GetOrderItems(323)", dataBase);
qDebug() << query.first();

它还返回所需的数据,并且query.first()是真实的,如预期的那样.

It also returns the desired data and query.first() is true like expected.

然后我尝试像Qt文档建议的那样使用参数绑定插入参数.我尝试了以下片段.第一个使用索引占位符,第二个使用索引占位符.

Then I tried to insert the parameter using parameter binding like the Qt documentation proposes. I tried the following snippets. The first uses index placeholder the second keyword placeholder.

QSqlQuery query(dataBase);
qDebug() << query.prepare("CALL GetOrderItems(?)");
query.bindValue(0, 323);
qDebug() << query.exec();
qDebug() << query.first();

QSqlQuery query(dataBase);
qDebug() << query.prepare("CALL GetOrderItems(:myparam)");
query.bindValue(":myparam", 323);
qDebug() << query.exec();
qDebug() << query.first();

这两个查询都可以正常执行.但是query.first()返回false,所以我不知道结果如何.

Both of these queries execute fine. But query.first() returns false so I don't know how the get the results.

我可以通过某种方式从绑定查询中获得结果吗?为什么这行不通?

Can I get the result from the binding queries in some way? Why doesn't this work?

推荐答案

在Qt文档中可以找到两个句子:

There are two sentences that can be found in the Qt documentation:

"MySQL 5引入了SQL级别的存储过程支持,但没有用于控制IN,OUT和INOUT参数的API." (摘自此处,谢谢Mat)

"MySQL 5 introduces stored procedure support at the SQL level, but no API to control IN, OUT and INOUT parameters." (from here, Thanks Mat)

不完全支持使用return语句返回值或返回多个结果集的存储过程." (摘自此处)

"Stored procedures that uses the return statement to return values, or return multiple result sets, are not fully supported." (from here)

因此,显然在Qt/MySQL中绑定参数是毫无用处的.另请参见有关批处理模式.

So obviously binding parameters in Qt/MySQL is pretty useless. See also this about batch mode.

这篇关于使用绑定调用服务器存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆