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

一、代碼以下:mysql

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); } }

 二、執行otl_stream stream2(100, sql2, otlConn,otl_implicit_select);的時候出錯,以下:sql

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

特別注意:若是test1 只返回1條或者0條記錄,不會致使這個異常。spa


三、錯誤緣由:mysql上一次的查詢沒有將結果集釋放掉,又進行下一次的查詢。
四、otl:在第一個stream讀取期間,第二個stream使用了綁定變量,會致使上面的問題,不知道otl內部是怎麼封裝的。
五、解決辦法:
a、第二個stream不使用綁定變量,以下:code

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,以下:blog

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); } }
相關文章
相關標籤/搜索