Getting the result of a query

Some queries give results but others don't. For example, the data results for UPDATE, INSERT, or DELETE statements always contain 0 rows. When running a SELECT statement, there may or may not be rows that matched your query, so it is always a good idea to check whether you have data by examining the return value of qdb_statement().

Note: You can still call qdb_getresult() for statements with 0 rows in the result set. In fact, it may be the only way to retrieve the result. If the connection was opened with the QDB_CONN_STMT_ASYNC flag set, then qdb_statement() returns before the statement has been completed (see "Using asynchronous mode"). With complex statements, this may mean a delayed error.

To help debug your application, you can print the fetched result to stdout() to visualize your data, using this call:

qdb_printmsg(stdout, result, QDB_FORMAT_SIMPLE);

Here's an example of getting the results of an operation:

 qdb_result_t *result;
 // requires a statement previously run
 result = qdb_getresult(dbhandle);
 if (result == NULL) {
    char *errmsg;
    errmsg = qdb_geterrmsg(dbhandle);
    fprintf(stderr, "Error getting result: %s\n", errmsg);

Memory for the results is allocated when the statement is run on the database, so you must free the result structure or you will have memory leaks. Do this by calling qdb_freeresult(), as shown in the example later in this chapter. Never call free() yourself.