2012-07-16 55 views
1

我們正在運行一個C程序,其中每一秒都有一個函數回調。下面的代碼列出的片段:C運行查詢顯示命令不同步?

char timeBuf[10],secondBuf1[100],queryBuf1[500],queryBuf2[500]; 
char buff[20] = {0}; 
struct timeval tv; 
gettimeofday (&tv, NULL); 
tv.tv_sec -= 5; 
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec)); 
printf("\nTime is %s", buff); 

sprintf(secondBuf1,"INSERT INTO secondsLog2 (secondLogID , timeStampID) VALUES (NULL,'%s')",buff); 
//printf("Query 1 before executing %s\n",queryBuf1); 
if (mysql_query(localConn, secondBuf1)) 
{ 
    printf("Error in insert of seconds log %s\n",mysql_error(localConn)); 
    exit(1); 
} 

sprintf(queryBuf1,"SELECT ipDest, portDest, SUM(packetLen), COUNT(ipDest) FROM source1 WHERE timeStamp = '%s' GROUP BY portDest",buff); 
printf("\nQuery buf %s",queryBuf1); 
if(mysql_query(remoteConn, queryBuf1)) 
{ 
    printf("Error in first query of select %s\n",mysql_error(remoteConn)); 
    exit(1); 
} 
localRes1 = mysql_use_result(remoteConn); 
while((localRow1 = mysql_fetch_row(localRes1)) !=NULL) 
      { 
       sprintf(queryBuf1,"INSERT INTO export1 (iBTID ,timeStampID ,ipDest ,portDest,totalBits, packetCount) VALUES (NULL,'%s','%s','%s',%s,%s)",buff, localRow1[0],localRow1[1],localRow1[2],localRow1[3],localRow1[4]); 
       printf("Query 1 before executing %s\n",queryBuf1); 
       if (mysql_query(localConn, queryBuf1)) 
       { 
       printf("Error in first query of insert %s\n",mysql_error(localConn)); 
       exit(1); 
       } 

      } 
         mysql_free_result(localRes1); 

我運行此腳本第二SELECT會給我這個錯誤的時刻:命令不同步;你現在不能運行此命令:

Time is 2012-07-17 00:59:14 
Query buf SELECT ipDest, portDest, SUM(packetLen), COUNT(ipDest) FROM source1 WHERE timeStamp = '2012-07-17 00:59:14' GROUP BY portDest 
Time is 2012-07-17 00:59:15 
Query buf SELECT ipDest, portDest, SUM(packetLen), COUNT(ipDest) FROM source1 WHERE timeStamp = '2012-07-17 00:59:15' GROUP BY portDestError in first query of select Commands out of sync; you can't run this command now 

回答

3

你需要清除你的結果上會返回結果有任何疑問設置。如果你不使用的結果的話,只要致電:

MYSQL_RES *results; 
results = mysql_store_result(localConn); 
mysql_free_result(results); 

要使用你的結果只是調用返回的結果,並確保他們在一些點以後使用mysql_free_result查詢後mysql_store_result(或mysql_use_result)。這應該會清除與CR_COMMANDS_OUT_OF_SYNC錯誤有關的任何問題。

從文檔mysql_store_result(強調):

After invoking mysql_query() or mysql_real_query(), you must call mysql_store_result() or mysql_use_result() for every statement that successfully produces a result set (SELECT, SHOW, DESCRIBE, EXPLAIN, CHECK TABLE, and so forth). You must also call mysql_free_result() after you are done with the result set.

+0

我已經更新了我的與被利用從之前的選擇查詢和INFACT我確實有這個了mysql_free_result(localRes1)的結果插入查詢的問題;但是在插入查詢之後。所以現在我應該在插入查詢之前釋放它,然後我將如何使用插入查詢的結果? – user837306 2012-07-16 17:51:42

+0

我試過這個localRes2 = localRes1;釋放localRes1之前,但在插入語句中沒有顯示任何內容? – user837306 2012-07-16 17:56:43

+0

所以最好解決我的插入問題以獲得結果,以確保我的選擇不再被卡住。我確實有mysql_store_result(remoteConn); ?那麼,那麼錯誤是什麼? – user837306 2012-07-16 17:58:46