熟悉C++调用mysql存储过程的看过来!

independently 2014-02-22 05:43:12
初学Mysql,创建了一个表和一个输出参数和返回结果集的存储过程。
表:
create table student
(
id varchar(32) not null,
name varchar(20) not null,
age tinyint unsigned not null,
home varchar(50) not null,
birthday datetime not null,
primary key(id)
)

存储过程:
create procedure p_querystudent
(inname varchar(32),
OUT OutTotalCount int unsigned) /*输出参数*/
begin
declare v_sqlselect varchar(4096);
declare v_sqlcount varchar(4096);
declare v_studentname varchar(512);
set v_sqlselect = 'select id,name,age,home,birthday from student where 1 = 1';
set v_sqlcount = 'select count(*) into @recordcount from student where 1 = 1';

if inname is NOT NULL and inname <> '' then
set v_studentname = concat(' and name like ''%',inname,'%''');
else
set v_studentname = '';
end if;

set v_sqlcount = concat(v_sqlcount, v_studentname);
set v_sqlselect = concat(v_sqlselect, v_studentname);
set @sqlcount = v_sqlcount;
set @sqlselect = v_sqlselect;
prepare stmtcount from @sqlcount;
prepare stmtselect from @sqlselect;
execute stmtcount;
execute stmtselect;
deallocate prepare stmtcount;
deallocate prepare stmtselect;
set OutTotalCount = @recordcount;
end;
C++调用过程:
连接数据库代码省略....

MYSQL_RES *res_ptr = NULL;
MYSQL_ROW sqlrow;
unsigned int nValue = 0;
char szSql[1024] = {0};
int nlen = sprintf(szSql,"call p_querystudent('',@nValue)");
int nRet = mysql_real_query(conn_ptr, szSql, nlen);
if(nRet)
{
printf("Query Error,erro_int[%d] erro_str[%s]\n", mysql_errno(conn_ptr), mysql_error(conn_ptr));
}
else
{
res_ptr = mysql_use_result(conn_ptr);
if(res_ptr)
{
while((sqlrow = mysql_fetch_row(res_ptr)))
{
unsigned int field_count = 0;
while(field_count < mysql_field_count(conn_ptr))
{
printf("%s ", sqlrow[field_count]);
field_count++;
}
printf("\n");
}

if(mysql_error(conn_ptr))
{
printf("Retrive Data Error:%s\n", mysql_error(conn_ptr));
}

mysql_free_result(res_ptr);
}
}
用C++调用该存储过程,已能获取结果集,但是不知道怎么得到输出参数@nValue?在网上搜索了下,也没有找到答案,希望有知道的告知下,谢谢!!
...全文
323 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
independently 2014-03-08
  • 打赏
  • 举报
回复
引用 2 楼 ACMAIN_CHM 的回复:
int nlen = sprintf(szSql,"call p_querystudent('',@nValue);"); 然后紧接着执行 "select @nValue;" 得到结果。
我这样试过,但是返回错误呀?并未得到 @nValue的值,能再详细说下吗??
WWWWA 2014-02-28
  • 打赏
  • 举报
回复
加上select @nValue 就是用取得结果集的方法,在DELPHI中也一样
ACMAIN_CHM 2014-02-27
  • 打赏
  • 举报
回复
int nlen = sprintf(szSql,"call p_querystudent('',@nValue);"); 然后紧接着执行 "select @nValue;" 得到结果。
independently 2014-02-27
  • 打赏
  • 举报
回复
悲哀。。。无一人能回答此问题!

56,865

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧