Prepared Statements¶
Prepared Statements can save SQL parsing time and improve performance.
Note
Now only support INSERT
, SELECT
UPDATE
DELETE
statements.
Steps¶
-
Create Prepared Statements
-
Method A: Execute with Args
-
Method B: Bind then Execute
- Bind parameters
NOTE Bind parameter id starts from 1.
- Execute prepared statements
-
Close statements
Insert¶
xdb_stmt_t *pStmt = xdb_stmt_prepare (pConn, "INSERT INTO student (id,name,age,class,score) VALUES (?,?,?,?,?)");
if (NULL != pStmt) {
pRes = xdb_stmt_bexec (pStmt, id, name, age, cls, score);
pRow = xdb_fetch_row (pRes);
// handle pRow
xdb_free_result (pRes);
// close when finish using
xdb_stmt_close (pStmt);
}
}
xdb_stmt_t *pStmt = xdb_stmt_prepare (pConn, "INSERT INTO student (id,name,age,class,score) VALUES (?,?,?,?,?)");
if (NULL != pStmt) {
xdb_bind_int (pStmt, 1, id);
xdb_bind_str (pStmt, 2, name);
xdb_bind_int (pStmt, 3, age);
xdb_bind_str (pStmt, 4, cls);
xdb_bind_int (pStmt, 5, score);
pRes = xdb_stmt_exec (pStmt);
pRow = xdb_fetch_row (pRes);
// handle pRow
xdb_free_result (pRes);
// close when finish using
xdb_stmt_close (pStmt);
}
}