Executing an SQL statement

Executing statements against a QDB database requires that you know and follow the QDB-supported SQL syntax, as described in the QDB SQL Reference chapter. You must, of course, connect to the database before attempting to execute statements against it.

One example is to run the following query:

int rc;
qdb_hdl_t    *dbhandle;

/* Code for connecting to the database to obtain a handle goes here */

rc = qdb_statement(dbhandle, "SELECT * FROM customers;");
if (rc == -1) {
    char *errmsg;
    errmsg = qdb_geterrmsg(dbhandle);
    fprintf(stderr, "QDB Error: %s\n", errmsg);
}

It is important to escape any strings that you pass in to qdb_statement(). For example, suppose you pass in this string:

SELECT lastname FROM customerdb WHERE lastname='O'Neil';

You would get an error because the second single quotation mark signals the end of the string and hence, the string in the WHERE clause would be interpreted as just 'O' while the remaining characters would produce an error. To correctly run the query, you must escape the single quotation mark in the middle of the string, as follows:

SELECT lastname FROM customerdb WHERE lastname='O''Neil';

The second single quotation mark (') terminates the string started with the first single quotation mark.