熟悉C++调用mysql存储过程的看过来!
初学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?在网上搜索了下,也没有找到答案,希望有知道的告知下,谢谢!!