MySQL Forums
Forum List  »  Connector/C (libmysql)

Commands out of sync; you can't run this command now
Posted by: Klas Furestig
Date: November 03, 2017 06:20AM

I have problem with "Commands out of sync; you can't run this command now" when I use prepare statement and C API. I have now created a little test program and try to reset the statement but I get "Commands out of sync; you can't run this command now".

The program exit with this message "stmt_reset fault query_1000:".

I have tried a lot of different combinations but I will get "Commands out of sync; you can't run this command now" all the time.

What have I not understood about Mysql and prepare statement?

/Klas Furestig

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include <my_global.h>
#include <mysql.h>

#define BUFFERSIZE 4096

//static MYSQL *con = NULL;
//MYSQL_STMT *stmt_1000 = NULL;

int main() {
MYSQL *con = NULL;
MYSQL_STMT *stmt_1000 = NULL;

int config_id = 1;
char host[256] = "l4407s.sss.se.*******.com";
unsigned long host_len = strlen(host);

char *query_1000 = "call show_config_host (?,?)";
char *query_1010 = "call show_config_host_job (?,?)";

con = mysql_init(NULL);

if (con == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}

if (mysql_real_connect(con, "localhost", "*******", "*******","inotify_test", 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

stmt_1000=mysql_stmt_init(con);

if (mysql_stmt_prepare(stmt_1000, query_1000, strlen(query_1000))) {
fprintf(stderr, "In prepare fault query_1000: %s\n", mysql_error(con));
exit(1);
};

MYSQL_BIND bind[2];
char *msg;

size_t len;

char dir_namn[256];
char fil_namn[256];
int watch_file_id;

unsigned long length[3];
my_bool error[3];
my_bool is_null[3];

MYSQL_BIND bind_out[3];

char *buffer;

memset(bind, 0, sizeof(bind));

bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].buffer=host;
bind[0].buffer_length= 256;
bind[0].is_null= 0;
bind[0].length= &host_len;

bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer=&config_id;
bind[1].is_null= 0;

if (mysql_stmt_bind_param(stmt_1000, bind)) {
fprintf(stderr, " mysql_stmt_bind_param() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt_1000));
exit(0);
}

if (mysql_stmt_execute(stmt_1000)) {
fprintf(stderr, "execute: %s\n", mysql_stmt_error(stmt_1000));
exit(1);
}

bind_out[0].buffer_type= MYSQL_TYPE_STRING;
bind_out[0].buffer= (char *)dir_namn;
bind_out[0].buffer_length= 255;
bind_out[0].is_null= &is_null[0];
bind_out[0].length= &length[0];
bind_out[0].error= &error[0];

bind_out[1].buffer_type= MYSQL_TYPE_STRING;
bind_out[1].buffer= (char *)fil_namn;
bind_out[1].buffer_length= 255;
bind_out[1].is_null= &is_null[1];
bind_out[1].length= &length[1];
bind_out[1].error= &error[1];

bind_out[2].buffer_type= MYSQL_TYPE_LONG;
bind_out[2].buffer=(char *)&watch_file_id;
bind_out[2].is_null= &is_null[2];
bind_out[2].error= &error[2];
bind_out[2].length= &length[2];

if (mysql_stmt_bind_result(stmt_1000, bind_out)) {
fprintf(stderr, " mysql_stmt_bind_result() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt_1000));
exit(1);
}


if (mysql_stmt_store_result(stmt_1000)) {
fprintf(stderr, " mysql_stmt_store_result() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt_1000));
exit(1);
}

buffer = malloc(BUFFERSIZE);
memset(buffer,0,BUFFERSIZE);

char *buffer_ptr = buffer;

char a_watch_file_id[10];

while (!mysql_stmt_fetch(stmt_1000)) {
memcpy(buffer_ptr,dir_namn,strlen(dir_namn));
buffer_ptr+=strlen(dir_namn);

memcpy(buffer_ptr,"\t",1);
buffer_ptr+=1;

memcpy(buffer_ptr,fil_namn,strlen(fil_namn));
buffer_ptr+=strlen(fil_namn);

memcpy(buffer_ptr,"\t",1);
buffer_ptr+=1;

sprintf(a_watch_file_id,"%d",watch_file_id);
memcpy(buffer_ptr,a_watch_file_id,strlen(a_watch_file_id));
buffer_ptr+=strlen(a_watch_file_id);

memcpy(buffer_ptr,"\n",1);
buffer_ptr+=1;
}

if (mysql_stmt_reset(stmt_1000)){
fprintf(stderr, "stmt_reset fault query_1000: %s\n", mysql_error(con));
exit(1);
};


if (mysql_stmt_prepare(stmt_1000, query_1010, strlen(query_1010))) {
fprintf(stderr, "In2 prepare fault query_1000: %s\n", mysql_error(con));
exit(1);
};

}

Options: ReplyQuote


Subject
Views
Written By
Posted
Commands out of sync; you can't run this command now
44
November 03, 2017 06:20AM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.