原文:c / c + + call mysql stored procedure
Create table student( id int auto_increment, name varchar(20), age tinyint, remark varchar(50), primary key(id) );
Insert into student values(1,"zhouys",90, ""); commit;
mysql> select * from student; +------+-----------+------+----------+ | id | name | age | remark | +------+-----------+------+----------+ | 1 | zhouys | 90 | | +------+-----------+------+-----------+ 1 row in set (0.00 sec) mysql>
delimiter // create procedure querystudent( in in_id int , #0- Character ID 1-digital id # out out_ret int, # Returns the result out out_name varchar(20), # First name out out_age int # Age ) label_a:begin declare v_name varchar(20) ; declare v_age tinyint ; # Parameters determine 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't found leave label_a; end if; set out_ret=0; set out_name=v_name; set out_age=v_age; end; // delimiter ;
if(!mysql_init(&mysql)) { printf("mysql_init failed!\n"); return 0; }
//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; }
//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));
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);
while((record = mysql_fetch_row(results))) { printf("[%s]-[%s]-[%s]\n", record[0], record[1],record[2]); }常规 的 存储 过程 将 只 返回 结果 ,^_^。
mysql_free_result(results); mysql_close(&mysql);
Mysql的存储过程可以实现相当强大的功能,这里只是一些基本的用法,希望能给你一些帮助。也欢迎分享......
像C#调用都可以绑定输出参数,直接通过输出参数得到值,但是c++中使用prepare绑定输出参数,却返回以下错误信息:
Error Code: 1414. OUT or INOUT argument 2 for routine knockout_dbo.ko_SignUpYoungBoxer is not a variable or NEW pseudo-variable in BEFORE trigger
以下链接是找到的相关问题讨论:
http://forums.mysql.com/read.php?38,505961
http://bugs.mysql.com/bug.php?id=25970
下面是一个c++的MySQL封装
http://tangentsoft.net/mysql++/