C++调用含输出参数的存储过程

independently 2014-01-08 09:42:23
初学Mysql,写了一个输出参数和返回结果集的存储过程,用C++调用该存储过程,已能获取结果集,但是不知道怎么得到输出参数?在网上搜索了下,也没有找到答案,希望有知道的告知下,谢谢!!
...全文
304 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iihero 2014-02-24
  • 打赏
  • 举报
回复
bind一个output变量试试。
independently 2014-02-22
  • 打赏
  • 举报
回复
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?
知道就是你 2014-01-12
  • 打赏
  • 举报
回复
引用 3 楼 independently 的回复:
[quote=引用 1 楼 JenMinZhang 的回复:] 详情参照 CSDN博客 : http://blog.csdn.net/ytz_linuxer/article/details/4435056 ,内容挺详细的,好好看看吧 !
mysql_query(&mysql, "SELECT @s "); 这个参数s该如何获取呢???[/quote] 这个是输出参数 ,直接在存储过程中 set 就可以了 。 //取得存储过程返回值 res = mysql_store_result(&mysql); 你到网查查看
independently 2014-01-11
  • 打赏
  • 举报
回复
引用 1 楼 JenMinZhang 的回复:
详情参照 CSDN博客 : http://blog.csdn.net/ytz_linuxer/article/details/4435056 ,内容挺详细的,好好看看吧 !
mysql_query(&mysql, "SELECT @s "); 这个参数s该如何获取呢???
iihero 2014-01-09
  • 打赏
  • 举报
回复
请参考我的这篇短文: http://blog.csdn.net/iihero/article/details/8362269 里边有存储过程相关调用及取结果的过程。
知道就是你 2014-01-09
  • 打赏
  • 举报
回复
详情参照 CSDN博客 : http://blog.csdn.net/ytz_linuxer/article/details/4435056 ,内容挺详细的,好好看看吧 !

56,687

社区成员

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

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