Recently done things using MySQL to do query sorting, but has been the problem, mainly occurs in the query when the memory is always increasing, each increase in memory size is the same,
Find a lot of information to know that the original was used Mysql_store_result () after forgetting to release :
So, remember that the result set by Mysql_store_result (), Mysql_use_result (), Mysql_list_dbs () must call Mysql_free_result () after completing the operation on the result set. Frees the memory used by the result set.
Also, the address of the result returned by each query is not the same, so it must be disposed.
Here are some of MySQL's operating functions
Go to: http://blog.csdn.net/bichenggui/article/details/5944102
The result set of MySQL is actually a mysql_res structure, which is defined as follows:
typedef struct st_mysql_res {
My_ulonglong Row_count; Number of rows in the result set
unsigned int field_count, Current_field; The number of columns in the result set, when the forefront
Mysql_field *fields; Column information for the result set
mysql_data *data; Data for the result set
mysql_rows *data_cursor; Cursor for result set
mem_root Field_alloc; Memory structure
Mysql_row ROW; used when not buffered.
Mysql_row Current_row; It will be used when mysql_store_result. When moving forward
unsigned long *lengths; The length of each column
MYSQL *handle; Mysql_use_result will use.
My_bool EOF; Is the end of the line
} mysql_res;
typedef char * *
Mysql_row; /* The value of each row returned, all in string notation */typedef struct St_mysql_rows {struct st_mysql_rows *next;/* List of rows */Mysql_row data;}
mysql_rows; A linked list node for MySQL data. The result set of the visible MySQL is the linked list structure typedef struct ST_MYSQL_DATA {my_ulonglong rows; unsigned int fields; Mysql_rows *data; Mem_root Alloc;}
Mysql_data; Structure of the dataset typedef struct St_mysql_field { char *name;/* Name of column */ char *table; , &NB Sp /* Table of column If column was a field */ char *def; &NBS P /* Default value (set by Mysql_list_fields) */ enum Enum_field_types t Ype /* 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; /* D IV flags */ unsigned INT decimals; /* number of decimal s in field */}
Mysql_field; Structure of column information
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 int min_malloc;
unsigned int block_size;
void (*error_handler) (void);
} mem_root; Memory structure
Here's the API for getting data:
1. Mysql_store_result ()
Mysql_res *mysql_store_result (MySQL *mysql)
Describe
For each query (SELECT, SHOW, DESCRIBE, EXPLAIN, CHECK table, and so on) that successfully retrieved the data, you must call Mysql_store_result () or Mysql_use_result ().
For other queries, you do not need to call Mysql_store_result () or Mysql_use_result (), but if you call Mysql_store_result () in any case, it will not cause any damage or performance degradation. You can detect whether a query has no result set (more later) by checking whether Mysql_store_result () returns 0.
If you want to know whether a query should return a result set, you can use Mysql_field_count () to check. See section 25.2, 3.22, "Mysql_field_count ()".
Mysql_store_result () reads all the results of the query to the client, allocates 1 mysql_res structures, and places the results in the structure.
If the query does not return a result set, Mysql_store_result () returns a null pointer (for example, if the query is an INSERT statement).
If the result set fails to be read, Mysql_store_result () also returns a null pointer. By checking whether mysql_error () returns a non-empty string, Mysql_errno () returns a non-0 value, or if Mysql_field_count () returns 0, you can check if an error has occurred.
If no rows are returned, an empty result set is returned. (An empty result set setting differs from a null pointer as a return value.)
Once Mysql_store_result () is called and a result is obtained that is not a null pointer, call Mysql_num_rows () to find the number of rows in the result set.
You can call Mysql_fetch_row () to get the rows in the result set, or call Mysql_row_seek () and Mysql_row_tell () to get or set the current row position in the result set.
Once you have completed the operation on the result set, you must call Mysql_free_result ().
2. Mysql_use_result ()
Mysql_res *mysql_use_result (MySQL *mysql)
Describe
For each query that successfully retrieves data (SELECT, SHOW, DESCRIBE, EXPLAIN), you must use Mysql_store_result () or Mysql_use_result ().
Mysql_use_result () Retrieves the initialization result set, but does not actually read the result set to the client like Mysql_store_result (). it must be retrieved for each row by a call to Mysql_fetch_row (). This will read the result directly from the server without saving it in a temporary table or in a local buffer, which is faster and uses less memory than Mysql_store_result (). the client allocates memory only for the current row and communication buffers, and the allocated memory can be increased to max_allowed_packet bytes.
On the other hand, if you are doing a lot of processing on the client side for each row, or sending the output to a screen where the user might type "^s" (stop scrolling), you should not use Mysql_use_result (). This binds the server and prevents other threads from updating any tables (data obtained from such tables).
When using Mysql_use_result (), the mysql_fetch_row () must be executed until a null value is returned, otherwise the fetched row is returned as part of the next retrieval . The C API gives the command out of sync error, and if you forget to perform the operation, you will not be able to run the command.
You cannot use Mysql_data_seek (), Mysql_row_seek (), Mysql_row_tell (), mysql_num_rows (), or mysql_affected_rows () for the result set, or you should not issue additional queries. Until Mysql_use_result () is complete. (However, after all rows have been fetched, mysql_num_rows () will return exactly the number of rows fetched).
Once you have completed the operation on the result set, you must call Mysql_free_result ().
return value
Mysql_res the result structure. If an error occurs, NULL is returned.
3. Obtain a row of data:
Mysql_fetch_row ()
Mysql_row mysql_fetch_row (Mysql_res *result)
Describe
Retrieves the next row of the result set. When used after Mysql_store_result (), mysql_fetch_row () returns null if there are no rows to retrieve.
When used after Mysql_use_result (), mysql_fetch_row () returns null if there are no rows to retrieve or an error has occurred.
The number of columns in the result set is given by Mysql_num_fields (result). If the row holds the value returned by calling Mysql_fetch_row (), a pointer to these values will be accessed according to Row[0] to Row[mysql_num_fields (result)-1]. Null values in a row are indicated by a null pointer.
You can get the length of the field value in the row by calling Mysql_fetch_lengths (). For empty fields and fields that contain null, the length is 0. You can differentiate them by checking the pointer to the field values. If the pointer is null, it is null, otherwise the field is empty.
return value
The mysql_row structure of the next line. Returns null if there are no more rows to retrieve or an error has occurred.
Example code:
Let's say MySQL is connected.
mysql_res* result = Mysql_store_result (MySQL);
if (result = = NULL)
Return
Mysql_row ROW;
unsigned int num_fields;
unsigned int i;
Num_fields = Mysql_num_fields (result);
while (row = mysql_fetch_row (result)))
{
unsigned long *lengths;
Lengths = Mysql_fetch_lengths (result);
for (i = 0; i < num_fields; i++)
{
printf ("[%.*s]", (int) lengths[i], Row[i]? Row[i]: "NULL");
}
printf ("/n");
}
4. Obtain the information for the column.
Mysql_fetch_field ()
Mysql_field *mysql_fetch_field (Mysql_res *result)
Describe
Returns the column that takes the result set of the Mysql_field structure. Call the function repeatedly to retrieve information about all the columns in the result set. Mysql_fetch_field () returns NULL when No field is left.
Each time a new select query is executed, Mysql_fetch_field () is reset to return information about the 1th field. Calling Mysql_field_seek () also affects the fields returned by Mysql_fetch_field ().
If mysql_query () is called to perform a select on the table, but Mysql_store_result () is not called, if Mysql_fetch_field () is called to request the length of the Blob field, MySQL will return the default blob length (8KB). 8KB is chosen because MySQL does not know the maximum length of the blob. It should be made configurable at a later date. Once the result set is retrieved, Field->max_length will contain the length of the maximum value for that column in a particular query.
return value
The Mysql_field structure of the forefront. If no column is left, NULL is returned.
Error
No.
Example:
Mysql_field *field;
while (field = Mysql_fetch_field (result)))
{
printf ("Field name%s/n", Field->name);
}
5. Same as 4.
Mysql_fetch_fields ()
Mysql_field *mysql_fetch_fields (Mysql_res *result)
Describe
For the result set, returns an array of all mysql_field structures. Each structure provides a field definition for the 1 columns in the result set.
return value
An array of Mysql_field structures for all columns of the result set.
Example:
unsigned int num_fields;
unsigned int i;
Mysql_field *fields;
Num_fields = Mysql_num_fields (result);
Fields = Mysql_fetch_fields (result);
for (i = 0; i < num_fields; i++)
{
printf ("Field%u is%s/n", I, fields[i].name);
}
6.
Mysql_free_result ()
void Mysql_free_result (Mysql_res *result)
Describe
Frees the memory allocated for the result set by Mysql_store_result (), Mysql_use_result (), Mysql_list_dbs (), and so on. After you complete the operation on the result set, you must call Mysql_free_result () to release the memory used by the result set.
When the release is complete, do not attempt to access the result set.
return value
No.
Always remember to release the result set!!!
About the memory leak problem that occurs with MySQL