Thursday, 7 February 2013

mysql commands out of sync after repair table

Recently I was trying to automate some table repairs when a server crash is detected. My code attempted to repair a number of tables but after repairing the first table subsequent repairs failed with "Commands out of sync; you can't run this command now".

On investigation I realised I actually needed to read the response from the database much as if I had queried a table.

It appears four columns are returned from the repair statement reflecting the status of the repair.

Example code:-

    sprintf(sql_statement,"repair table %s",tablename);
    reportlog(sql_statement);
    if (mysql_query (local, sql_statement) != 0) {
        sprintf(message,"mysql select failed:\nError %u %s\n",mysql_errno(local), mysql_error(local));
        reportlog(message);
    }

    res_set = mysql_store_result(local);
    if (res_set != NULL) {
        row = mysql_fetch_row (res_set);
        if (row != NULL) {
            sprintf(message,"%s %s %s %s",row[0],row[1],row[2],row[3]);
            reportlog(message);
        }
    }   
    mysql_free_result(res_set);

Running the code results in :-

database.tablename repair status OK


No comments: