mysql c api(二)

由于直接执行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

 

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:

 

 

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.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值