Mysql c api learning Summary

Source: Internet
Author: User

1. query result set

(1) Data Structure

Typedef struct st_mysql_res {

My_ulonglong row_count; // number of rows in the result set (long or long)

Unsigned intfield_count, current_field; // Number of columns in the result set, current column

MYSQL_FIELD * fields; // The column information of the result set.

MYSQL_DATA * data; // data of the result set

MYSQL_ROWS * data_cursor; // The cursor of the result set

MEM_ROOT field_alloc; // Memory Structure

MYSQL_ROW row; // used for non-buffering

MYSQL_ROW current_row; // mysql_store_result is used. Current row

Unsigned long * lengths; // The length of each column

MYSQL * handle; // mysql_use_result will be used.

My_bool eof; // whether it is the end of the row

} MYSQL_RES;

 

Typedef struct st_mysql_field {char * name;/* Name of column */char * table;/* Table of column if column was a field */char * def; /* Default value (set by mysql_list_fields) */enum enum_field_types type;/* Type of field. se mysql_com.h for types */unsigned int length;/* Width of column */unsigned int max_length;/* Max width of selected set */unsigned int flags; /* Div flags */unsigned Int decimals;/* Number of decimals in field */} MYSQL_FIELD; // structure of the column information: typedef struct st_mysql_data {my_ulonglong rows; unsigned int fields; MYSQL_ROWS * data; MEM_ROOT alloc ;} MYSQL_DATA; // The dataset structure typedef struct st_mysql_rows {struct st_mysql_rows * next;/* list of rows */MYSQL_ROW data;} MYSQL_ROWS; // The linked list node of mysql data. It can be seen that the mysql result set is the value of each row returned by the chain table structure typedef char ** MYSQL_ROW;/*, all represented by strings */

Typedef struct st_used_mem {/* struct for once_alloc */

Struct st_used_mem * next;/* Next block in use */

Unsigned int left;/* memory left in block */

Unsigned int size;/* Size of block */

} USED_MEM; // Memory Structure

 

Typedef struct st_mem_root {

USED_MEM * free;

USED_MEM * used;

USED_MEM * pre_alloc;

Unsigned intmin_malloc;

Unsigned intblock_size;

Void (* error_handler) (void );

} MEM_ROOT; // Memory Structure

(2) API

1. mysql_store_result ()

MYSQL_RES * mysql_store_result (MYSQL * mysql)

For each query that successfully retrieves data (SELECT,SHOW,DESCRIBE,EXPLAIN,CHECK TABLEAnd so on), Must be calledMysql_store_result ()OrMysql_use_result ().

Other queries do not need to be calledMysql_store_result()OrMysql_use_result()But if you callMysql_store_result (),It does not cause any damage or performance degradation. Pass CheckMysql_store_result ()Return?0To check whether the query has no result set.(More in the future ).

You can useMysql_field_count()Check

Mysql_store_result ()Read all the query results to the client,Allocate1ItemsMYSQL_RESStructure,And place the result in this structure.

If no result set is returned for the query,Mysql_store_result ()ReturnsNullPointer (for example, if the query isINSERTStatement).

If the read result set fails,Mysql_store_result ()Will returnNullPointer. Pass CheckMysql_error ()Whether to return a non-null string,Mysql_errno ()Whether to return non-0Value, orMysql_field_count ()Return?0You can check for errors.

If no row is returned, an empty result set is returned. (Null result set settings are different from null pointers used as return values ).

Once calledMysql_store_result ()And obtainedNullPointer result,CallableMysql_num_rows()To find the number of rows in the result set.

YesMysql_fetch_row ()To obtain or call rows in the result set.Mysql_row_seek()AndMysql_row_tell()To obtain or set the current row position in the result set.

You must callMysql_free_result().

 

2.Mysql_use_result

MYSQL_RES *Mysql_use_result(MYSQL * mysql)

Description

For each query (SELECT,SHOW,DESCRIBE,EXPLAIN), Must be usedMysql_store_result ()OrMysql_use_result ().

Mysql_use_result ()Will initialize the result set retrieval, but not likeMysql_store_result ()In this way, the result set is actually read to the client.It must passMysql_fetch_row. This will directly read the results from the server, instead of saving them in a temporary table or local buffer, which is faster and uses less memory than mysql_store_result.The client only allocates memory for the current row and communication buffer,The allocated memory can be increasedMax_allowed_packetBytes.

On the other hand, if you are processing a large number of operations for each row on the client side, or sending the output to the user, you may type"^ S"(Stop scrolling) the screen should not be usedMysql_use_result (). This binds the server and prevents other threads from updating any table (data is obtained from such tables ).

UseMysql_use_result ()Must be executedMysql_fetch_row ()Until returnNULLValue. Otherwise,Unretrieved rows are returned as part of the next search.C APIThe command is not synchronized. If you forget to execute this operation, you cannotRun this command.

Cannot Use mysql_data_seek () for the result set (),Mysql_row_seek (),Mysql_row_tell (),Mysql_num_rows ()OrMysql_affected_rows (), Other queries should not be sentMysql_use_result ()Complete. (However, after extracting all rows,Mysql_num_rows ()Returns the number of extracted rows ).

Once the operation on the result set is completed, mysql_free_result () must be called ().

Return Value

MYSQL_RESResult structure.If an error occurs, returnNULL.

 

3.

Mysql_fetch_row

MYSQL_ROWMysql_fetch_row(MYSQL_RES * result)

Description

The next row of the result set. InMysql_store_result ()If there are no rows to be retrieved during subsequent use,Mysql_fetch_row ()ReturnNULL.

InMysql_use_result ()In later use,If no row to be retrieved or an error occurs,Mysql_fetch_row ()ReturnNULL.

The number of columns in the result set is determinedMysql_num_fields (result). If the call is saved in the rowMysql_fetch_row ()The returned value will followRow [0]ToRow [mysql_num_fields (result)-1]To access the pointers of these values. In the rowNULLValue RangeNULLPointer.

You can callMysql_fetch_lengths ()To obtain the length of the field value in the row. For empty fields and includingNULL, The length is0. By checking the pointer of Field Values, You can differentiate them. If the pointer isNULL, The field isNULLOtherwise, the field is empty.

Return Value

Next lineMYSQL_ROWStructure. If there are no more rows to be retrieved or an error occursNULL.

4.Mysql_num_rows

My_ulonglongMysql_num_rows (MYSQL_RES*Result)

The use of mysql_num_rows () depends on whether mysql_store_result () or mysql_use_result () is used to return the result set. If mysql_store_result () is used, you can call mysql_num_rows () immediately (). If mysql_use_result () and mysql_num_rows () are used, the correct value is not returned until all rows in the result set are retrieved.

5.Mysql_num_fields

Unsigned intMysql_num_fields(MYSQL_RES * result)

You can obtain the number of rows from the pointer to the result set or the pointer to the connection handle. IfMysql_store_result ()OrMysql_use_result ()ReturnNULL,The connection handle should be used (so there is no result set pointer ). In this case, you can callMysql_field_count ()To judgeMysql_store_result ()Whether a non-empty result is generated. In this way, the client program can take appropriate actions without knowing whether the query isSELECTStatement (or similarSELECTStatement ).The following example describes how to perform this operation.

6.Mysql_fetch_row

MYSQL_ROW mysql_fetch_row (MYSQL_RES * result)

The next row of the result set. InMysql_store_result ()If there are no rows to be retrieved during subsequent use,Mysql_fetch_row ()ReturnNULL. InMysql_use_result ()In later use,If no row to be retrieved or an error occurs,Mysql_fetch_row ()ReturnNULL.

The number of intra-row values is determinedMysql_num_fields (result). If the call is saved in the rowMysql_fetch_row ()The returned value will followRow [0]ToRow [mysql_num_fields (result)-1]To access the pointers of these values. In the rowNULLValue RangeNULLPointer.

You can callMysql_fetch_lengths ()To obtain the length of the field value in the row. For empty fields and includingNULL, The length is0. By checking the pointer of Field Values, You can differentiate them. If the pointer isNULL, The field isNULLOtherwise, the field is empty.

Return Value

Next lineMYSQL_ROWStructure. If there are no more rows to be retrieved or an error occursNULL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.