MySQL query results in ergodic common API (c)

As used in this table:

MySQL error handling function

 unsigned int mysql_errno(MYSQL *mysql) 

 const char *mysql_error(MYSQL *mysql) 

Description: A function that returns the error number, a return error message, both to MYSQL * as a parameter, it is intuitive.

The first step: query operation

Used functions:

int mysql_query(MYSQL *mysql, const char *stmt_str) 

int mysql_real_query(MYSQL *mysql, const char *stmt_str, unsigned longlength) 

Return Value: Returns 0 on success, failure returns nonzero if it fails, you can take advantage of error-handling function to get the error message.

The difference between these two functions: When the query using binary data, only with the latter.

 

Step two: save query results (only the select statement with query results, update, delete, insert no query results)

Used functions:

 MYSQL_RES *mysql_store_result(MYSQL *mysql) 

 MYSQL_RES *mysql_use_result(MYSQL *mysql) 

Return Value: If successful, return a non-null pointer MYSQL_RES, otherwise, it returns NULL. Note that, as long as the successful implementation of the select statement, even if there is no matching entry to the select statement, the return value of the function should not be NULL.

The difference between the two functions: mysql_store_result the entire result set into the client stores, select the query results if there are one hundred entries, mysql_store_result one hundred entries which will then call all present in the memory, but only from mysql_use_result remove the result set in memory in a table entry, the next entry is taken from the function server when the result set using mysql_fetch_row (). So mysql_use_result use less memory and faster execution. But if you need some processing of the data on the client side of the result set, then still with mysql_store_set it.

Summary: mysql_store_result select statement will result in the execution of all server to keep the client's memory, and mysql_use_result need to use the data only when the next data taken from the server to the client stored in the memory.

Note: The results used up, use mysql_free_result () to free the memory . The prototype of this function is  void mysql_free_result (A MYSQL_RES * Result) 

 The third step: to traverse results

Through the results can be divided into two steps: firstly fetches a row in the result set of data, and then through each column of data in the line data.

Traversal function row used:

 MYSQL_ROW mysql_fetch_row(MYSQL_RES *result) 

Description: This above step query result and returns a MYSQL_ROW structure, this structure is a row of data result set in, can be understood as an element of the character array of pointers (char * row [elems]) , when all the data in the result set is traversed completed, the function returns NULL. So how do you use this structure it? Firstly prepared by the following function to obtain the result set (mysql_num_fields () / mysql_field_count () ) the number of columns , and then starting from index 0, to (the number of columns --1) ends, so traversing it. It refers to the current generation of col columns, if the current column is NULL, the value MYSQL_ROW [col] also to NULL.

Example of use:

 1 MYSQL_ROW row;
 2 unsigned int cols = mysql_field_count(conn_ptr);
 3 while ((row = mysql_fetch_row(res_ptr)) != NULL)
 4 {
 5     for (unsigned int i = 0; i < cols; ++i)
 6     {
 7         printf("%s ", row[i]);
 8     }
 9     printf("\n");
10 }

 

Output:

In fact, under normal circumstances should row [i] whether the judgment is NULL, here I was a little puzzled why the row [i] in the case is empty, the result is also OK normal printf, without creating a segmentation fault.

 1 MYSQL_ROW row;
 2 unsigned int cols = mysql_field_count(conn_ptr);
 3 while((row = mysql_fetch_row(res_ptr)) != NULL)
 4 {      
 5     for(unsigned int i = 0;i < cols;++i)
 6     {  
 7         if(row[i] == NULL)
 8             printf("i am null");
 9         else                                     
10             printf("%s ",row[i]);
11     }   
12     printf("\n");
13 }   

result:

Traversal function is used when the column:

 unsigned int mysql_num_fields(MYSQL_RES *result) 

 unsigned int mysql_field_count(MYSQL *mysql) 

 MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result) 

For the first two functions, the parameters are different, but the effect is the same, the number of columns of the result set is returned in the current query.

Attribute columns can be obtained by the third function (including the column name, a column type information), and its use mysql_fetch_col () similar, while also need to use a continuous loop. When the return value is null, no represents the next column.

Example of use:

 1  MYSQL_RES* res_ptr = mysql_store_result(conn_ptr);
 2  if(res_ptr)
 3  {
 4      printf("use result successful\n");
 5      
 6      MYSQL_FIELD* field;
 7      while((field = mysql_fetch_field(res_ptr)))
 8      {
 9          printf("%s ",field->name);
10      }
11      printf("\n");
12 
13  }

result: 

 All codes

 1 #include <mysql/mysql.h>
 2 
 3 #include <stdlib.h>
 4 #include <stdio.h>
 5 
 6 int main()
 7 {
 8     //MYSQL* mysql_init(MYSQL *mysql)
 9     MYSQL* conn_ptr = mysql_init(NULL);
10     if(conn_ptr == NULL)
11     {
12         fprintf(stderr,"mysql_init() failed\n");
13         return -1;
14     }
15     //MYSQL* mysql_real_connect(MYSQL *mysql, const char *host,
16     //const char *user,const char *passwd, const char *db,
17     //unsigned int port, const char *unix_socket, unsigned long clientflag) 
18     mysql_real_connect(conn_ptr,"localhost",
19                                   "rick","secretpassword","rick",
20                                   0,NULL,0);
21     if(conn_ptr)
22     {
23         printf("connect successfully\n");
24         int query_ret = mysql_query(conn_ptr,"SELECT * FROM children;");
25         if(query_ret == 0)
26         {
27             printf("query successful\n");
28 
29             MYSQL_RES* res_ptr = mysql_store_result(conn_ptr);
30             if(res_ptr)
31             {
32                 printf("use result successful\n");
33 
34                 MYSQL_FIELD* field;
35                 while((field = mysql_fetch_field(res_ptr)))
36                 {
37                     printf("%s ",field->name);
38                 }                                                
39                 printf("\n");
40 
41                 MYSQL_ROW row;
42                 unsigned int cols = mysql_field_count(conn_ptr);
43                 while((row = mysql_fetch_row(res_ptr)) != NULL)
44                 {
45                     for(unsigned int i = 0;i < cols;++i)
46                     {
47                         printf("%s ",row[i]);
48                     }
49                     printf("\n");
50                 }
51             }
52             else
53             {
54                 fprintf(stderr,"use result failed %d : %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr));
55             }
56             mysql_free_result(res_ptr);
57         }
58         else
59         {
60             fprintf(stderr,"query failed %d : %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr));
61         }
62 
63     }
64     else
65     {
66         printf("connect failed\n");
67     }
68     //void mysql_close(MYSQL *sock)
69     mysql_close(conn_ptr);
70     return 0;
71 }

 

Guess you like

Origin www.cnblogs.com/XiaoXiaoShuai-/p/11923311.html