c++ - 连接器/C++ MySQL 错误代码 : 2014 , SQLState : HY000 and Commands out of sync error why?

标签 c++ mysql client

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

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 

最佳答案

一次连接上不能有多个事件查询。

来自mysql_use_result文档:

You may not use mysql_data_seek(), mysql_row_seek(), mysql_row_tell(), mysql_num_rows(), or mysql_affected_rows() with a result returned from mysql_use_result(), nor may you issue other queries until mysql_use_result() has finished.

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

关于c++ - 连接器/C++ MySQL 错误代码 : 2014 , SQLState : HY000 and Commands out of sync error why?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6826139/

相关文章:

c++ - 从没有默认构造函数的纯抽象类继承

c++ - cin.getline() 跳过

c# - .net 中的高性能 TCP 客户端

php - 如何在 php 中将评论值附加到各个帖子

c - 错误: dereferencing pointer to incomplete type是什么意思

java - 将默认 Java VM 更改为客户端

c++ - 为指针/共享指针和其他类型重载流 << 运算符

c++ - connect() 后 boost 异常

php - 对 mysql 的搜索结果进行排序

mysql - 如何在 magento 中对 mysql 进行转义?