求助:存储过程后out of sync
最近遇到一个存储过程问题,需要在存储过程返回数据给C代码使用。也释放了result的查询结果,但是在后续执行的mysql 查询的时候还是会遇到out of sync(errno:2014)错误。
具体代码如下:
存储过程
delimiter //
drop procedure if exists pbx_r_eid_svcid;
CREATE PROCEDURE `pbx_r_eid_svcid`(IN input_eid varchar(16))
begin
declare exe_result int default -1;
declare out_svcip varchar(64) default "";
declare out_svcport varchar(64) default "";
select exe_result,out_svcip,out_svcport;
end;//
delimiter ;
C代码:
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
MYSQL *mysql;
int timeout=atoi(argv[1]);
char escape[64] = "";
char orig[64] = "";
char buf[128] = "";
int rc = 0;
MYSQL_RES *res;
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_OPT_READ_TIMEOUT, &timeout);
mysql_options(mysql, MYSQL_OPT_WRITE_TIMEOUT, &timeout);
mysql_real_connect(mysql, "127.0.0.1", "root", "123456", "center_distribute", 3306,"/var/run/mysqld/mysqld.sock", CLIENT_FOUND_ROWS | CLIENT_MULTI_STATEMENTS);
snprintf(buf,128, "call pbx_r_eid_svcid('00000002')", "11");
if (mysql_real_query(mysql,buf,strlen(buf)))
{
int err_no = mysql_errno(mysql);
fprintf(stderr, "Failed to insert row, Error: %d %s\n", err_no,
mysql_error(mysql));
}
res = mysql_store_result(mysql);
if(0 == mysql_affected_rows(mysql) || res == NULL)
{
rc = 6;
goto end;
}
mysql_free_result(res);
/*随意一条查询语句*/
snprintf(buf,128,"select * from enterprise_info;");
/*wrong query return out of sync*/
if (mysql_real_query(mysql,buf,strlen(buf)))
{
int err_no = mysql_errno(mysql);
fprintf(stderr, "Failed to insert row, Error: %d %s\n", err_no,
mysql_error(mysql));
}
res = mysql_store_result(mysql);
if(0 == mysql_affected_rows(mysql) || res == NULL)
{
rc = 7;
goto end;
}
mysql_free_result(res);
end:
printf("%d\n",rc);
mysql_close(mysql);
return 0;
}
编译
gcc -g -o mt mysql_test.c -lmysqlclient
执行结果
./mt 5
Failed to insert row, Error: 2014 Commands out of sync; you can't run this command now
7