why can't retrieved result by call stored procedure using c api?
Posted by:
wang fletcher ()
Date: May 11, 2006 09:30PM
hello,everyone ,I am a new one from china.
I encounter a problem when i write a c code to test how to call stored procedure in mysql!
But I can't retrieved the results,the follow is the c source code:
int main(int argc, char* argv[])
{
MYSQL handle;
mysql_init(&handle);
if(!mysql_real_connect(&handle, "localhost", "root", NULL,
"Geopacket", 0, NULL,
CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS)){
printf("mysql_real_connect error\n");
mysql_close(&handle);
return -1;
}
const char* sqlQuery = "SET @@max_sp_recursion_depth=100; SET @msg = '111111'; CALL agent_tree(@msg);";
if(mysql_real_query(&handle, sqlQuery, (unsigned long) strlen(sqlQuery))){
printf("mysql_real_query error\n");
mysql_close(&handle);
return -1;
}
printf("mysql_real_query: %s\n", sqlQuery);
MYSQL_RES* result=NULL;
do{
printf("=================================\n");
result = mysql_store_result(&handle);
if(NULL == result){
printf("Empty resultset retrieved\n");
continue;
}
MYSQL_ROW row = mysql_fetch_row(result);
if(row != NULL){
MYSQL_FIELD *mysqlFields = mysql_fetch_fields(result);
const char* field_name = mysqlFields[0].name;
unsigned long* field_lengths = mysql_fetch_lengths(result);
unsigned long field_length = field_lengths[0];
char* fieldData = (char *)malloc(field_length + 1);
memset(fieldData, '\0', field_length + 1);
memcpy(fieldData, row[0], field_length);
printf("%s: %s.\n", field_name, fieldData);
free(fieldData);
}
mysql_free_result(result);
}while(mysql_next_result(&handle) == 0);
mysql_free_result(result);
mysql_close(&handle);
return 0;
}
when I excute the code,this is the result:
#./a.out
mysql_real_query: SET @@max_sp_recursion_depth=100;
SET @msg = '111111'; CALL agent_tree(@msg);
=================================
Empty resultset retrieved
=================================
Empty resultset retrieved
=================================
Account_number: 111111.
=================================
Empty resultset retrieved
#
But when I call the stored procedure in mysql command line,the return
result is OK:
mysql> call agent_tree(@msg);
+----------------+--------+-----------+
| Account_number | Name | Parent_id |
+----------------+--------+-----------+
| 111111 | host | 000000 |
| 111112 | Agent1 | 111111 |
| 111115 | Agent4 | 111112 |
| 111117 | Agent6 | 111115 |
| 111113 | Agent2 | 111111 |
| 111116 | Agent5 | 111113 |
| 111114 | Agent3 | 111111 |
+----------------+--------+-----------+
7 rows in set (0.01 sec)
Query OK, 0 rows affected, 1 warning (0.01 sec)
what's wrong? Are there some tips about calling stored procedure using c api?
please help me,thanks