qdb_stmt_init()
Initialize a prepared statement
Synopsis:
#include <qdb/qdb.h> int qdb_stmt_init( qdb_hdl_t *db, const char *sql, uint32_t len );
Arguments:
- db
- A pointer to the database handle.
- sql
- An SQL statement. This statement may contain variable parameters of the form ?n, where n is a number between 1 and 999. These placeholders can be filled in with data on a subsequent call to qdb_stmt_exec().
- len
- The length of sql.
Library:
qdbDescription:
This function initializes a prepared (precompiled) SQL statement. A prepared statement is compiled once but can be executed multiple times. This function returns a statement ID for the prepared statement, which you must then pass in to qdb_stmt_exec().
QDB executes precompiled statements faster than uncompiled statements, so this approach can optimize your application's performance when executing frequently used statements.
You can free prepared statements using qdb_stmt_free(), although all such statements are freed when you call qdb_disconnect().
Returns:
- >=0
- Success. The returned value is the prepared statement's ID. Note that 0 is a valid ID.
- -1
- An error occurred (errno is set).
Examples:
The following code sample shows how to compile, execute, and free an SQL statement:
int stmtid;
qdb_binding_t qbind[2];
uint64_t msid, limit;
const char *sql = "SELECT fid FROM library WHERE msid=?1 LIMIT ?2;";
stmtid = qdb_stmt_init(db, sql, strlen(sql)+1);
if (stmtid == -1) {
// Could not compile
return -1;
}
msid = 1;
limit = 10;
QDB_SETBIND_INT(&qbind[0], 1, msid);
QDB_SETBIND_INT(&qbind[1], 2, limit);
if (qdb_stmt_exec(db, stmtid, qbind, 2) == -1) {
// Could not execute
return -1;
}
qdb_stmt_free(db, stmtid);
Note the +1 added to the length of the string returned by
strlen() (which is used in the call to
qdb_stmt_init()); this sends QDB the final NULL
character required for a valid string.
Classification:
QNX Neutrino
| Safety: | |
|---|---|
| Interrupt handler | No |
| Signal handler | No |
| Thread | Yes |
