C++オペレーションストレージプロセス2014の問題MySQL


オペレーションストアドプロシージャ接続コード
エラーコード:
Commands out of sync; you can't run this command now
DB_Param mDB("192.168.1.254", "admin", "admin", "node", 3306, NULL, CLIENT_MULTI_STATEMENTS );//CLIENT_MULTI_RESULTS);
	CMysql mysql(mDB);

bool CMysql::ConnectDB(DB_Param* p)
{
    if(!mysql_real_connect(&mysql,(p->mStrHost).c_str(),p->mStrUser.c_str(),p->mStrPassword.c_str(),p->mStrDB.c_str(),p->mIPort,p->unix_socket,p->client_flag))
    {
        return false;
    }
    return true;
}

上の最後のパラメータは、
CLIENT_MULTI_STATEMENTS )  CLIENT_MULTI_RESULTS)

C++操作MySQLストレージ中、次のエラーが発生しました.
Commands out of sync; you can't run this command now

理由はMYSQL_RES結果は次のクエリで解放されなかったため、このエラーが発生しました
公式の解釈は以下の通りです.
Mysqlドキュメントの説明エラー:Commands out of sync
If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.
This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.
 
queryを実行した後、mysqlは結果セットを1つのresultセットに配置し、以上の問題の原因は2つあります.
一つはMYSQL_をRESは、次のクエリでmysql_を使用してオブジェクトを解放します.free_result();
2つ目は、結果resultセットが空ではないため、隠蔽されています.解決策は次のように書くことができます.
do

{

  /* Process all results */

  printf("total affected rows: %lld", mysql_affected_rows(mysql));

  ...

  if (!(result= mysql_store_result(mysql)))

  {

     printf(stderr, "Got fatal error processing query/n");

     exit(1);

  }

  process_result_set(result); /* client function */

  mysql_free_result(result);

} while (!mysql_next_result(mysql));
上のコードは私のプログラムで成功しませんでした.
私の解決方法:2つ
1つ目の方法:クエリー関数を呼び出して、1回解放して、毎回データベースを操作して、すべて再び1回のデータベースに接続して、この操作を使い終わったらすぐにMYSQL_を解放しますRES結果セット
この方法では、データベースの操作が頻繁で、効率が相対的に高くありません.
第2の方法:すべてのデータをキャッシュに読み込み、キャッシュを読み書きし、効率が高く、空間が時間を変える方法.
どちらの方法にもそれぞれの解決方法があり、あなたのプログラムの実際の状況に依存します.