Executing a 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. See "Connecting to the database".

One example is to run the following query:

 int rc;
 qdb_hdl_t    *dbhandle;
 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, if you pass in the string:

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

you would get an error, because the string in the WHERE clause would be interpreted as just 'O', because the second single quotation mark signals the end of the string, and the remaining characters produce an error. To correctly run the query, 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 (') is escaped by the first single quotation mark.