使用otl,报错:mysql Commands out of sync; you can't run this command now

1、代码如下:

void TestCache(otl_connect& otlConn)
{
    try
    {
        char sql[1024] = {0};
        sprintf(sql,"call test1(1)");
        otl_stream stream(100, sql, otlConn,otl_implicit_select);

        int id;
        while(!stream.eof())
        {
            stream>>id;
            char sql2[1024] = {0};
            sprintf(sql2,"call test2(:Id<int>)");
            otl_stream stream2(100, sql2, otlConn,otl_implicit_select);
            stream2<<id;
            
       int ff =0;
while(!stream2.eof()) { stream2>>ff; } } } catch(otl_exception& ex) { printf("ExecuteSql Error, ErrorMsg[%s], Sql[%s]", ex.msg, ex.stm_text); } }

 2、执行otl_stream stream2(100, sql2, otlConn,otl_implicit_select);的时候出错,如下:

Commands out of sync; you can't run this command now

特别注意:如果test1 只返回1条或者0条记录,不会导致这个异常。


3、错误原因:mysql上一次的查询没有将结果集释放掉,又进行下一次的查询。
4、otl:在第一个stream读取期间,第二个stream使用了绑定变量,会导致上面的问题,不知道otl内部是怎么封装的。
5、解决办法:
a、第二个stream不使用绑定变量,如下:

void TestCache(otl_connect& otlConn)
{
    try
    {
        char sql[1024] = {0};
        sprintf(sql,"call test1(1)");
        otl_stream stream(100, sql, otlConn,otl_implicit_select);

        int id;
        while(!stream.eof())
        {
            stream>>id;
            char sql2[1024] = {0};
            sprintf(sql2,"call test2(%d)",id);
            otl_stream stream2(100, sql2, otlConn,otl_implicit_select);

            int ff =0;
while(!stream2.eof()) { stream2>>ff; } } } catch(otl_exception& ex) { printf("ExecuteSql Error, ErrorMsg[%s], Sql[%s]", ex.msg, ex.stm_text); } }

b、先把第一个stream读取完,再进行第二个stream,如下:

void TestCache(otl_connect& otlConn)
{
    try
    {
        char sql[1024] = {0};
        sprintf(sql,"call test1(1)");
        otl_stream stream(100, sql, otlConn,otl_implicit_select);

        vector<int> intVec;
        int id;
        while(!stream.eof())
        {
            stream>>id;
            intVec.push_back(id);            
        }

        for(vector<int>::iterator iter = intVec.begin();
            iter != intVec.end(); ++iter)
        {
            char sql2[1024] = {0};
            sprintf(sql2,"call test2(:Id<int>)");
            otl_stream stream2(100, sql2, otlConn,otl_implicit_select);
            stream2<<id;

            int ff =0;
while(!stream2.eof()) { stream>>ff; } } } catch(otl_exception& ex) { printf("ExecuteSql Error, ErrorMsg[%s], Sql[%s]", ex.msg, ex.stm_text); } }

 

 

posted on 2015-05-30 17:52  Andy Niu  阅读(8151)  评论(0编辑  收藏  举报