由于直接执行SQL语句的效率比较低,所以还有另外一种叫预处理的执行方式。
基本数据结构:
MYSQL_STMT:预处理语句的句柄
MYSQL_BIND:用于输入输出参数的绑定,主要成员变量:buffer_type,buffer,buffer_length
基本方法:
MYSQL_STMT *mysql_stmt_init(MYSQL *mysql):用于创建预处理语句句柄
Create a MYSQL_STMT
handle. The handle should be freed with mysql_stmt_close(MYSQL_STMT *)
.
A pointer to a MYSQL_STMT
structure in case of success. NULL
if out of memory.
CR_OUT_OF_MEMORY
Out of memory.
int mysql_stmt_prepare(MYSQL_STMT *stmt, const char *stmt_str, unsigned long length):对预处理语句进行预处理
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.
Zero if the statement was prepared successfully. Nonzero if an error occurred.
my_bool mysql_stmt_bind_param(MYSQL_STMT *stmt, MYSQL_BIND *bind):输入参数绑定
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. Whenmysql_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 tomysql_stmt_execute()
for a statement such asSELECT
that produces a result set.
mysql_stmt_fetch()
returns row data using the buffers bound bymysql_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 thelength
pointer. All columns must be bound by the application before it callsmysql_stmt_fetch()
.
my_bool mysql_stmt_close(MYSQL_STMT *):释放stmt空间
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.
Zero if the statement was freed successfully. Nonzero if an error occurred.