使用 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());