系统:Windows 7 pro;数据库版本:mysql 5.5.18.使用Mysql C API编写了如下的函数:int Func(void)
{
    MYSQL mysqlObj;
    MYSQL_STMT *stmt;
    char *sqlSyntax = "insert into my_table(id, name) values(?,?)";
    mysql_real_connect(&mysqlObj, "localhost", "root", "1234", "my_db", 0, NULL, 0);
    stmt = mysql_stmt_init(&mysqlObj);    mysql_stmt_prepare(stmt, sqlSyntax, (unsigned long)strlen(sqlSyntax));
    
...
}调试程序发现:连接数据库等语句执行是正常的,但是当执行到mysql_stmt_prepare()这句时, 函数报了一个错误:Commands out of sync; you can't run this command now。请高手给与帮助,谢谢。

解决方案 »

  1.   

    将 MYSQL mysqlObj;改为 MYSQL *mysqlObj 试试呢?
    因为我也没用过C语言链接Mysql,所以刚刚查了一下api
    api有说明ooo
    http://zetcode.com/tutorials/mysqlcapitutorial/
      

  2.   

    执行语句 mysql_stmt_prepare()之前,必须使用mysql_stmt_bind_param(),将参数标记符与应用程序变量绑定在一起。不然会报错 “Commands out of sync;” (以不恰当的顺序执行了命令。)