Statements with mi_exec_prepared_statement()
The mi_exec_prepared_statement() function is for the execution of prepared statements, both queries and other valid SQL statements.
DataBlade® API function | Step in prepared-statement execution |
---|---|
mi_prepare() | Prepares the statement string for execution |
mi_statement_command_name(), mi_get_statement_row_desc(), or input-parameter accessor function (Input-parameter information in the statement descriptor) | Obtains information about the prepared statement (optional) |
mi_exec_prepared_statement() | Sends the prepared statement to the database server for execution and opens any cursor required |
mi_drop_prepared_statement() | Releases prepared-statement resources |
- Binds any input-parameter values to the appropriate input parameters
in the prepared statement
For more information, see Assign values to input parameters.
- Sends the prepared statement to the database server for execution
The control flag supports the MI_BINARY flag to indicate that query rows are to be returned in binary representation. For more information, see Determine control mode for query data.
- When it executes a query, it performs the following additional
steps:
- Opens an implicit cursor to hold the query rows
- Reads the query rows into the open cursor
The DataBlade® API stores the cursor as part of the statement descriptor. For more information about this row cursor, see Queries and implicit cursors.
When the mi_exec_prepared_statement() function successfully fetches the query rows into the cursor, the cursor position points to the first row of the cursor, and the mi_get_result() function returns a status of MI_ROWS to indicate that the cursor contains rows.
You can access these rows one at a time with the mi_next_row() function. Each access obtains the row to which the cursor position points. After each access to the cursor, the cursor position moves to the next row. For more information, see Retrieving query data.
mi_integer send_statement2(conn, stmt)
MI_CONNECTION *conn;
mi_string *stmt;
{
mi_integer count;
MI_STATEMENT *stmt_desc;
/* Prepare the statement */
if ( (stmt_desc = mi_prepare(conn, stmt, NULL)) == NULL )
mi_db_error_raise(conn, MI_EXCEPTION,
"mi_prepared failed\n");
/* Send the basic statement, specifying that query
* be sent in its text representation
*/
if ( mi_exec_prepared_statement(stmt_desc, 0, MI_FALSE,
0, NULL, NULL, NULL, 0, NULL) == MI_ERROR )
mi_db_error_raise(conn, MI_EXCEPTION,
"mi_exec_prepared_statement failed\n");
/* Get the results of the current statement */
count = get_results(conn);
/* Release statement resources */
if ( mi_drop_prepared_statement(stmt_desc) == MI_ERROR )
mi_db_error_raise(conn, MI_EXCEPTION,
"mi_drop_prepared_statement failed\n");
if ( mi_query_finish(conn) == MI_ERROR )
mi_db_error_raise(conn, MI_EXCEPTION,
"mi_query_finish failed\n");
return ( count );
}
- On the first call to mi_exec_prepared_statement(), specify in the types array the correct data type names for the input parameters.
- On subsequent calls to mi_exec_prepared_statement(), replace the array of data type names with a NULL-valued pointer.
This method saves on the number of type descriptors that mi_exec_prepared_statement() must allocate, thereby reducing memory usage.