select count(*)from使用C语言中的MySQL函数给出错误的结果

时间:2018-01-05 09:47:50

标签: mysql c libmysql

我试图拒绝。 Oracle DB中表的条目。该应用程序是在C和我开发的,使用mySql lib来访问数据库。正如我所说,我在应用程序的db层和mysql函数中使用Oracle,这是使用libmysqlora完成的。除了一件事,当我发出类似' select count(*)from这样的查询时,我只得到实际计数的前3位数。以下是我的完整代码:

int64_t vmsdb_read_count(char *table, char *query)
{
uint i;
MYSQL_ROW row;
boolean free_query = FALSE;

if (query == NULL) {
    newvallocate(char, query, SHORT_QUERY_SIZE);
    snprintf(query, SHORT_QUERY_SIZE, "select count(*) from %s", table);
    free_query = TRUE;
}

if (mysql_query(mysql, query)) {
    exiterr(Q_NOTICE, query);
    goto ERROR;
}

if (!(res = mysql_store_result(mysql))) {
    exiterr(Q_NOTICE, "MySQL failed to store results. ");
    goto ERROR;
}

while((row = mysql_fetch_row(res))) {
    for (i=0 ; i < mysql_num_fields(res); i++) {
        int64_t count = atoll(row[i]);
        mysql_free_result(res);
        res =  NULL;
        if (free_query) {
            vfree(query);
            free_query = FALSE;
        }
        return (count);
    }
}
mysql_free_result(res);
res = NULL;
ERROR:
if (free_query) {
    vfree(query);
}
return 0;
}

对于表格&#39;目的地&#39;,当我计算sqlplus中的行时:

 SQL> select count(*) from destination;

 17928

但是当我使用上述函数时:

  printf("No. of rows in destination table = %d\n", vmsdb_read_count("destination", NULL));

  output--> No. of rows in destination table = 179

不仅对于此表,而且对于我的数据库中的每个其他表,该函数仅返回计数值中的前三位数。这是什么原因?而且,我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

此功能

int64_t vmsdb_read_count(char *table, char *query)

返回64位无符号整数。

此打印格式字符串

  printf("No. of rows in destination table = %d\n", ...

提供d转换说明符,它需要(签名)int。后者(最有可能)是32位。

在任何情况下,提供的值都与转换说明符不匹配,后者会调用未定义的行为。