-2

I'm trying to get no. of entries from a table in Oracle DB. The application is developed in C and I'm using mySql lib to access the database. As I stated, I am using Oracle in the db layer and mysql functions in the application, this is done using libmysqlora. Everything is working perfectly except one thing, when I issue the query like 'select count(*) from , I get only first 3 digits of the actual count. Following is my entire code to read this:

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;
}

For table 'destination', when I count rows in sqlplus:

 SQL> select count(*) from destination;

 17928

but When I use the aforementioned function for this:

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

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

Not just for this table, but for every other table in my database, the function only returns first three digits in the count value. What is the cause of this? And, how can I fix this issue?

Mat
  • 202,337
  • 40
  • 393
  • 406
padmanabh pande
  • 367
  • 2
  • 4
  • 21
  • When you talk about libmysqlora, is that a typo for [liboramysql](https://docs.oracle.com/database/121/DRDAA/mysql_driver.htm#DRDAA29191)? – JeremyP Jan 05 '18 at 10:02
  • 1
    Read the spec for printf. In particular re %d. Also [mcve]. – philipxy Jan 05 '18 at 10:45
  • I agree that printf statement is incorrect, but the reason I asked this question is different. the return value from this function is itself wrong, and that I can see in gdb as well. – padmanabh pande Jan 05 '18 at 11:02

1 Answers1

0

This function

int64_t vmsdb_read_count(char *table, char *query)

returns a 64bit unsigned integer.

This print format string

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

provides d conversion specifier, which expects a (signed) int. The latter (most likely) is 32bit.

In any case the value provide does not match the conversion specifier, which invoke undefined behaviour.

alk
  • 69,737
  • 10
  • 105
  • 255
  • I agree that printf statement is incorrect, but the reason I asked this question is different. the return value from this function is itself wrong, and that I can see in gdb as well. – padmanabh pande Jan 05 '18 at 11:02
  • @padmanabhpande: Which value do you see for `int64_t count` inside `vmsdb_read_count()` using GDB? – alk Jan 05 '18 at 11:12
  • Inside vmsdb_read_count("destination", NULL), I see 179 as count instead of 17928. For any other tables in the database, I see only first 3 digits of the actual count value. – padmanabh pande Jan 07 '18 at 10:23