使用 Connector/C++ 查询 Mysql , 连续调用存储过程时mysql
会出现以下:sql
Commands out of sync; you can't run this command now,state:HY000
出现缘由能够看这里:http://stackoverflow.com/questions/17115919/mysql-commands-out-of-sync-error-c-connector
函数
这一句:oop
Because CALL can return multiple results, process them using a loop that calls mysql_next_result() to determine whether there are more results. "
Connector/C++ 封装后,使用 getMoreResults() 函数来读取下一个 resultset;this
例子以下:spa
// 查询存储过程 prepareState.reset(con->prepareStatement("call test.testproc1(?)")); prepareState->setInt(1,1001); prepareState->executeUpdate(); result.reset(prepareState->getResultSet()); // 输出结果 while(result->next()) { int id = result->getInt("id"); string name = result->getString("name"); } while(prepareState->getMoreResults()) //注意这里, 调用 getMoreResults() 把全部的 resultset读出来 { result.reset(prepareState->getResultSet()); //对下一组result set 作某些东西 } prepareState.reset(con->prepareStatement("call test.testproc3(?)")); prepareState->setInt(1,1001); prepareState->executeUpdate(); result.reset(prepareState->getResultSet()); // 输出结果 while(result->next()) { int id = result->getInt("id"); string name = result->getString("name"); } while (prepareState->getMoreResults()) result.reset(prepareState->getResultSet());