首页 > 解决方案 > 在 C 中第二次调用存储过程时命令不同步 MySQL

问题描述

我使用 mySQL 创建了一个数据库,现在我需要启动我创建的存储过程,编写一个基本的 C 客户端,它要求一个命令,并调用存储过程。

特别是,在登录后,虽然我没有输入“10”来注销,但我的程序会询问我想调用哪个 SP。那是我的代码:

void root_logged ()
    {
        while (true)
        {   
            scanf ("%i",&cmd2);
            if (cmd2 == 2)
            {
                show_exams();
            }
            else if (cmd2 == 10)
            {
                do_logout();
                break;
            }
        }
    }


void show_exams ()
{
    strcpy(query,"call show_exams()"); //call mySQL SP
    mysql_query (conn,query);
    result = mysql_store_result(conn);

    if (result == NULL)
    {
        finish_with_error(conn, "errore");
    }

    printf ("check 2:\n");
    num_fields = mysql_num_fields(result);
    printf ("check 3 \n");
    while ((row = mysql_fetch_row(result))) 
    { 
        for(int i = 0; i < num_fields; i++) 
        {   
            if (i == 0) 
            {              
                while(field = mysql_fetch_field(result))
                {
                    printf( "|  %s  ", field->name);
                }
                printf("\n");           
            }
            printf(" %s ", row[i] ? row[i] : "NULL"); 
        }
        printf ("\n");
    }
    mysql_free_result(result);
}

如果我在登录后第一次输入“2”,它会正确显示整个表格“考试”,其中存储了所有数据。

结果之后,程序要求另一个命令。如果我再次输入 2 (或除 10 之外的任何其他 cmd 以注销),它会给我这个错误。

命令不同步;你现在不能运行这个命令

任何想法?我认为问题在于它about mysql_store_result()没有mysql_free_result()正确使用

标签: mysqlcdatabasesynchronizationconnector

解决方案


推荐阅读