Getting the result of a query

Some queries give results, and 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 make sure that you have data by checking the return value of qdb_statement().

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

To help you debug your application, you can use qdb_printmsg(stdout, result, QDB_FORMAT_SIMPLE) to print the fetched result to stdout() so that you can visualize your data.

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.