【问题标题】:MySQL C++ connector MySQL_Prepared_Statement::getUpdateCount errorMySQL C++ 连接器 MySQL_Prepared_Statement::getUpdateCount 错误
【发布时间】:2012-02-02 14:16:43
【问题描述】:

我正在使用 MySQL C++ 连接器版本 1.1.0。 这就是我的代码的样子:

PreparedStatement *pStatement;
connection->setAutoCommit(false);

pStatement = connection->prepareStatement("UPDATE records "
            "SET is_processed = ? "
            "WHERE id = ?");

    //LOOP BEGIN
    pStatement->setInt(1, is_processed);
    pStatement->setString(2, record_id);

    pStatement->execute();
    //LOOP END

int updated_records;

try 
{
    updated_records = pStatement->getUpdateCount();
}
catch(SQLException&e)
{
    cout << "ERROR: " << e.what();
    cout << " (MySQL error code: " << e.getErrorCode();
    cout << ", SQLState: " << e.getSQLState() << ")" << endl;
}

connection->commit();
connection->setAutoCommit(true);

抛出异常,输出如下:

ERROR: MySQL_Prepared_Statement::getUpdateCount (MySQL error code: 0, SQLState: )

所以它什么也没说。 getUpdateCount() 函数有什么问题?有什么方法可以获得更详细的错误报告级别?

编辑

还有其他方法可以使用 mysql c++ 连接器获取更新的行数吗?

【问题讨论】:

    标签: c++ mysql-connector


    【解决方案1】:

    我也遇到了这个问题。最后我看了一下源代码。看来源明确会抛出异常。

    uint64_t
    MySQL_Prepared_Statement::getUpdateCount()
    {
      checkClosed();
      throw MethodNotImplementedException("MySQL_Prepared_Statement::getUpdateCount");
      return 0; // fool compilers
    }
    

    【讨论】:

      【解决方案2】:

      我终于找到了可行的解决方案:

      int updated_records = 0;
      
          //LOOP BEGIN
          pStatement->setInt(1, is_processed);
          pStatement->setString(2, record_id);
      
          updated_records += pStatement->executeUpdate();
          //LOOP END
      
      cout << updated_records;
      

      executeUpdate() 返回受影响的行数,它可以正常工作,没有任何错误,所以这对我来说已经足够了。

      【讨论】:

        【解决方案3】:

        为别人澄清,因为我很困惑;这样做:

        pstmt = con->prepareStatement ("UPDATE localdata SET Val = ? WHERE ID = ?");
        pstmt->setDouble (1, 7.77);    // first "?"
        pstmt->setInt (2, 0);    // second "?"
        pstmt->executeUpdate ();
        

        诀窍是根据它们的表类型和语句中的顺序设置值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-07
          相关资源
          最近更新 更多