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

來源:互聯網
上載者:User

標籤:

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

 

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.