Search code examples
codbcdynamic-arrays

C ODBC save sql results in a dynamical array


I develop a small C++ tool which fill a xml from an SQL result. I use the C ODBC libraries to connect to the SQL server. This function get the records:

...
char result[5][64]; //More flexible 
...

for (i = 0; i < columns; i++) {
    SQLBindCol(stmt, i + 1, SQL_C_CHAR, result[i], sizeof(result[i]), &indicator[i]);
}

while (SQL_SUCCEEDED(SQLFetch(stmt))) {
    for (i = 0; i < columns; i++) {
         if (indicator[ i ] == SQL_NULL_DATA) {
            cout << format("Column %1% : NULL") % i << endl;
         }
         else {
            cout << format("Column %1% : %2%") % i % result[i] << endl;
         }
    }
}

I save the result in the result char array. I would like to do this in a more dynamically way.. To be save if there are more records or the values are longer. I know about malloc in C and new in C++, but how could I use this in my case? Any idea? Should I switch to something line tiodbc?


Solution

  • First count the number of records which are coming from the database then use the malloc to allocate the memory of that size.I think it would me more dynamic