由於直接執行SQL語句的效率比較低,所以還有另外一種叫預處理的執行方式。
基本數據結構:
MYSQL_STMT:預處理語句的句柄
MYSQL_BIND:用於輸入輸出參數的綁定,主要成員變量:buffer_type,buffer,buffer_length
基本方法:
MYSQL_STMT *mysql_stmt_init(MYSQL *mysql):用於創建預處理語句句柄
Description
Create a MYSQL_STMT
handle. The handle should be freed with mysql_stmt_close(MYSQL_STMT *)
.
Return Values
A pointer to a MYSQL_STMT
structure in case of success. NULL
if out of memory.
Errors
-
Out of memory.
int mysql_stmt_prepare(MYSQL_STMT *stmt, const char *stmt_str, unsigned long length):對預處理語句進行預處理
Description
Given the statement handle returned by mysql_stmt_init()
, prepares the SQL statement pointed to by the string stmt_str
and returns a status value. The string length should be given by the length
argument. The string must consist of a single SQL statement. You should not add a terminating semicolon (“;
”) or /g
to the statement.
Return Values
Zero if the statement was prepared successfully. Nonzero if an error occurred.
my_bool mysql_stmt_bind_param(MYSQL_STMT *stmt, MYSQL_BIND *bind):輸入參數綁定
Description
mysql_stmt_bind_param()
is used to bind input data for the parameter markers in the SQL statement that was passed to mysql_stmt_prepare()
. It uses MYSQL_BIND
structures to supply the data. bind
is the address of an array of MYSQL_BIND
structures. The client library expects the array to contain one element for each “?
” parameter marker that is present in the query.
Suppose that you prepare the following statement:
INSERT INTO mytbl VALUES(?,?,?)
When you bind the parameters, the array of MYSQL_BIND
structures must contain three elements, and can be declared like this:
MYSQL_BIND bind[3];
Return Values
Zero if the bind operation was successful. Nonzero if an error occurred.
my_bool mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind):綁定輸出參數,如果是字符串類型,一定要指定長度,不然會輸出錯誤結果。
Description
mysql_stmt_bind_result()
is used to associate (that is, bind) output columns in the result set to data buffers and length buffers. When mysql_stmt_fetch()
is called to fetch data, the MySQL client/server protocol places the data for the bound columns into the specified buffers.
int mysql_stmt_execute(MYSQL_STMT *stmt):執行SQL語句
Description
mysql_stmt_execute()
executes the prepared query associated with the statement handle. The currently bound parameter marker values are sent to server during this call, and the server replaces the markers with this newly supplied data.
Statement processing following mysql_stmt_execute()
depends on the type of statement:
For an
UPDATE
,DELETE
, orINSERT
, the number of changed, deleted, or inserted rows can be found by callingmysql_stmt_affected_rows()
.For a statement such as
SELECT
that generates a result set, you must callmysql_stmt_fetch()
to fetch the data prior to calling any other functions that result in query processing.
int mysql_stmt_fetch(MYSQL_STMT *stmt):獲取一行記錄,信息保存在之前綁定的結果中。
Description
mysql_stmt_fetch()
returns the next row in the result set. It can be called only while the result set exists; that is, after a call to mysql_stmt_execute()
for a statement such as SELECT
that produces a result set.
mysql_stmt_fetch()
returns row data using the buffers bound by mysql_stmt_bind_result()
. It returns the data in those buffers for all the columns in the current row set and the lengths are returned to the length
pointer. All columns must be bound by the application before it calls mysql_stmt_fetch()
.
my_bool mysql_stmt_close(MYSQL_STMT *):釋放stmt空間
Description
Closes the prepared statement. mysql_stmt_close()
also deallocates the statement handle pointed to by stmt
.
If the current statement has pending or unread results, this function cancels them so that the next query can be executed.
Return Values
Zero if the statement was freed successfully. Nonzero if an error occurred.