mysql - C 运行查询显示命令不同步?

标签 mysql sql 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 会给我这个错误:Commands out of sync;你现在不能运行这个命令:

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

最佳答案

您需要清除任何将返回结果的查询的结果集。如果您不使用结果,则只需调用:

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

要使用您的结果,只需在查询返回结果后调用 mysql_store_result(或 mysql_use_result),并确保在某些时候对它们使用 mysql_free_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.

关于mysql - C 运行查询显示命令不同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509139/

相关文章:

MySql,如何选择3列中不重复的值

php - 使用正则表达式搜索并替换数据库中存在的电子邮件 ID

mysql - 使用子查询计算百分比

mysql - 测试驱动开发检查数据库查询涉及的方法

mysql - 查询限制不起作用

c - 分配结构

mysql - 由于某种原因,输出不断告诉我组函数的使用无效,即使我没有使用它

sql - 如何将一个表中的最新行连接到另一个表中?

c - Linux 中的 fork() 函数

c - 构建一个复杂的字符串?