c 调用mysql 存储过程_c/c++调用mysql存储过程

Create table student(

id int auto_increment,

name varchar(20),

age tinyint,

remark varchar(50),

primary key(id)

);

2、插入几条信息

Insert into student values(1,"zhouys",90, "");

commit;

3、查看用户信息

mysql> select * from student;

+------+-----------+------+----------+

| id | name | age | remark |

+------+-----------+------+----------+

| 1 | zhouys | 90 | |

+------+-----------+------+-----------+

1 row in set (0.00 sec)

mysql>

4、创建存储过程

如下:

delimiter //

create procedure querystudent(

in in_id int , #0-字符id 1-数字id

#

out out_ret int, #返回结果

out out_name varchar(20), #名字

out out_age int #年龄

)

label_a:begin

declare v_name varchar(20) ;

declare v_age tinyint ;

#参数判断

if (in_id<=0) then

set out_ret=-1; #id error

leave label_a;

end if;

SELECT name,age into v_name,v_age from student where id=in_id limit 1;

if v_age is NULL then

set out_ret=-2; #don't found

leave label_a;

end if;

set out_ret=0;

set out_name=v_name;

set out_age=v_age;

end;

//

delimiter ;

5、c语言调用存储过程

调用方法或步骤:

5.1、初始化Mysql句柄

if(!mysql_init(&mysql))

{

printf("mysql_init failed!\n");

return 0;

}

5.2、连接到mysql

//login or connect

if(!mysql_real_connect(&mysql,"localhost","root","","billingdb",0,NULL,CLIENT_MULTI_STATEMENTS))

{

printf("mysql_real_connect() failed!\n");

mysql_close(&mysql);

return 0;

}

5.3、调用存储过程

//call

strcpy(query,"call querystudent (1,@ret,@ out_name,@ out_age)");

printf("query sql=[%s]\n",query);

ret= mysql_real_query(&mysql,query,(unsigned int)strlen(query));

5.4、查询结果集并保存

mysql_query(&mysql, "SELECT @ret,@ out_name,@ out_age ");

//get result

if (ret)

{

printf("Error exec query: %s\n",mysql_error(&mysql));

}

else

{

printf("[%s] exec...\n", query);

}

results = mysql_store_result(&mysql);

5.5、获取查询结果

while((record = mysql_fetch_row(results))) {

printf("[%s]-[%s]-[%s]\n", record[0], record[1],record[2]);

}

一般存储过程只会有一行的返回结果,^_^.

5.6、释放资源与mysql连接句柄

mysql_free_result(results);

mysql_close(&mysql);

6、结束语

Mysql存储过程可以实现相当强大的功能,这里只是抛砖引玉的做一些实践,期望能够给大家一些帮助。也欢迎大家交流…

——转

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-07-26 19:03

浏览 1068

分类:数据库

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值