首页 > 数据库技术 > 详细

MySql: ”Commands out of sync“Error (Connect/C++)

时间:2015-09-10 21:08:55      阅读:225      评论:0      收藏:0      [点我收藏+]

使用 Connector/C++ 查询 Mysql , 连续调用存储过程时

会出现如下:

Commands out of sync; you cant run this command now,state:HY000

 

出现原因可以看这里:http://stackoverflow.com/questions/17115919/mysql-commands-out-of-sync-error-c-connector

这一句:

Because CALL can return multiple results, process them using a loop that calls mysql_next_result() to determine whether there are more results. "

Connector/C++ 封装后,使用  getMoreResults() 函数来读取下一个 resultset;

例子如下:

     // 查询存储过程
        prepareState.reset(con->prepareStatement("call test.testproc1(?)"));
        prepareState->setInt(1,1001);
        prepareState->executeUpdate();

        result.reset(prepareState->getResultSet());
        // 输出结果    
        while(result->next())
        {
            int id = result->getInt("id");
            string name = result->getString("name");
        }

        while(prepareState->getMoreResults())                            //注意这里, 调用 getMoreResults() 把所有的 resultset读出来
        {
            result.reset(prepareState->getResultSet());
            
            //对下一组result set 做某些东西
        }
      
        prepareState.reset(con->prepareStatement("call test.testproc3(?)"));
        prepareState->setInt(1,1001);
        prepareState->executeUpdate();

        result.reset(prepareState->getResultSet());
        // 输出结果    
        while(result->next())
        {
            int id = result->getInt("id");
            string name = result->getString("name");
        }

        while (prepareState->getMoreResults())
            result.reset(prepareState->getResultSet());

 

MySql: ”Commands out of sync“Error (Connect/C++)

原文:http://www.cnblogs.com/sixbeauty/p/4798879.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!