Connector/C ++ MySQL错误代码:2014,SQLState:HY000和命令不同步错误为什么? [英] Connector/C++ MySQL error code: 2014 , SQLState: HY000 and Commands out of sync error why?

查看:66
本文介绍了Connector/C ++ MySQL错误代码:2014,SQLState:HY000和命令不同步错误为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用Connector/C ++并执行了2个简单的sql命令,如下所示:第一个选择sql运行正常,但是第二个导致此异常错误:

Hi im using Connector/C++ and executing simple 2 sql commands like this : the first select sql run ok but the second one cause this exception error :

ERR:命令不同步;您不能立即运行此命令(MySQL错误代码:2014,SQLState:HY000)

ERR: Commands out of sync; you can't run this comman d now (MySQL error code: 2014, SQLState: HY000 )

这是代码:

 //member of the class 
 ResultSet *temp_res;
 // in different method 
 m_driver = get_driver_instance();
 m_con = m_driver->connect(m_DBhost,m_User,m_Password); 
 m_con->setSchema(m_Database);

//here i excute the querys :
vector<string> query;
query.push_back("SELECT * FROM info_tbl");
query.push_back("INSERT INTO info_tbl (id,name,age)VALUES (0,foo,36)");
query.push_back("SELECT * FROM info_tbl");

ResultSet *res;
Statement *stmt;     
bool stmtVal = false;

    try{
        stmt = m_con->createStatement();
        for(size_t i = 0;i < querys.size();i++)
        {
            string query = querys.at(i);
            stmtVal = stmt->execute(query);

            if(!stmtVal)
            {

                string error_log ="sql statment:";
                error_log.append(query);
                error_log.append(" failed!");

                cout << error_log << endl;
                break;

            }
        }
        if(stmtVal)
        {
            if(returnSet)
            {
                    res = stmt->getResultSet();
                    temp_res = res;              
            }
        }



        delete stmt;
        //close connection to db 
        m_con->close();
} catch (sql::SQLException &e) {
    ......
}

根据建议更新新代码(不起作用)

for(size_t i = 0;i < querys.size();i++)
        {
            string query = querys.at(i);
            stmtVal = stmt->execute(query);
            if(stmtVal)
            {
                if(returnSet)
                {
                    if(stmt->getResultSet()->rowsCount() > 0)
                    {
                        res = stmt->getResultSet();
                        temp_res = res;              
                    }
                    else
                    {       

                        delete res;
                    }
                }
                else 
                {
                    delete res;
                }
            }
            if(!stmtVal)
            {

                string error_log ="sql statment:";
                error_log.append(query);
                error_log.append(" failed!");

                cout << error_log << endl;
                break;

            }
        }

这是我的简单表格:

Column  Type        Null     
id          int(10)     No           
name    varchar(255)    No           
age     int(10)     No 

推荐答案

您一次在一个连接上最多只能有一个活动查询.

You can't have more than one active query on a connection at a time.

mysql_use_result 文档:

您不能将mysql_data_seek(),mysql_row_seek(),mysql_row_tell(),mysql_num_rows()或mysql_affected_rows()与mysql_use_result()返回的结果一起使用,也不能发出其他查询,直到mysql_use_result()具有完成.

这与您使用的不完全相同,但是问题是相同的-您需要先处理完第一个 ResultSet 并清理它,然后才能对该连接发出任何其他查询

That's not exactly what you're using, but the problem is the same - you'll need to finish processing the first ResultSet and clean it up before you can issue any other query on that connection.

这篇关于Connector/C ++ MySQL错误代码:2014,SQLState:HY000和命令不同步错误为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆