Mysql C API Query Process Introduction
2005-3-28 Keep_thinking
1. Overview of the query of the Mysql C API is mainly divided into two types of SQL statements consisting of SQL statements, including the query of the SQL statement of binary data. The first type of query only needs to pass the SQL statement to the query function, the second type of query not only needs to pass the SQL statement, but also to the length of the SQL statement. Obviously, the first class query does not need to pass the length, because the system knows NULL to indicate the end of the string. The second query must pass the length of the SQL statement, because there is a value that is a null in the SQL statement containing binary data. If still in accordance with the first policy, it is clear that the length of the query statement will exist. Truncate the danger of truncation. The following respectively give each of the first type inquiry and second type inquiries.
Second, the query of the SQL statement that does not contain binary data First, please take a rough look at the code, the following analysis.
Void DB_DO_QUERY (MySQL * DB, Const Char * Query)
{
IF (MySQL_Query (DB, Query)! = 0)
Goto ERR;
IF (mysql_field_count (db)> 0)
{
Mysql_res * res;
Mysql_row rotion;
INT NUM_FIELDS;
IF (! (res = mysql_store_result (db)))
Goto ERR;
Num_fields = mysql_num_fields (res);
While ((rot = mysql_fetch_row (res)))))
{
For (int i = 0; i IF (row == null) Cout << "null"; Else COUT << Row [i] << ''; Cout << Endl; } MySQL_Free_Result (res); } Else (void) Printf ("Affected Rows:% LLD / N", MySQL_AFFECTED_ROWS (DB)); Return; Err: DIE (DB, "DB_DO_QUERY FAILED:% S []", MySQL_ERROR (DB), Query; } For queries that do not contain binary data, the most core is the above code 1. MySQL_QUERY is the Mysql query function which requires two parameters, one is a pointer to the MySQL structure type, and the other is a string containing the SQL query statement. The mysql_query function itself does not return the result after the query, and the result is required to call another function mysql_store_Result. 2. The return value of the function mysql_store_result is a pointer to the Mysql_RES structure, which points to the query result. But you still can't print each data item directly through MySQL_RES. 3. You need to call the mysql_fetch_row function to get a pointer to the data item of each query result, which needs to be repeatedly called, returns a line of pointers per call. In the above code, repeatedly called by a cycle statement until the end of the result table. In addition, you can also use the mysql_num_rows function to return to the query result set how many lines of data. 4. The result returned by the mysql_fetch_row function is a pointer for a MySQL_ROW structure, which can be seen as a string array that sets the value of each domain of the row of query results, which can be indexed by the middle bracket operator. 5. There is also a problem at this time. How many domains have you got? This value can be obtained by a function mysql_num_fields. Third, the query that passes the SQL statement containing binary data first looks at the following code, pay attention to the source code of the query of the SQL statement listed in the above-mentioned SQL statement. Void DB_DO_REAL_QUERY (mysql * db, const char * query, unsigned int LEN) { IF (MySQL_Query (DB, Query)! = 0) Goto ERR; IF (mysql_field_count (db)> 0) { Mysql_res * res; Mysql_row rotion; INT NUM_FIELDS; Ulong * lengths; // The length of the column data used to store result sets IF (! (res = mysql_store_result (db))) Goto ERR; Num_fields = mysql_num_fields (res); While ((rot = mysql_fetch_row (res))))) { Lengths = mysql_fetch_lengths (res); For (int i = 0; i IF (row == null) Cout << "null"; Else // The data address is Row [i], the length is Lengths [i] Cout << Endl; } MySQL_Free_Result (res); } Else (void) Printf ("Affected Rows:% LLD / N", MySQL_AFFECTED_ROWS (DB)); Return; Err: DIE (DB, "DB_DO_QUERY FAILED:% S []", MySQL_ERROR (DB), Query; } There are only two different places: 1. The function needs to pass the length of the SQL statement of the query, which is already described above. 2. It is a need for a ULONG type pointer to store the width of the result set data. Fourth, two other questions 1. About MySQL_Store_Result and Two MySQL_USE_RESULT functions, these two functions are functions for getting the query result set, and the first one is used in the example program, which can be replaced with the second. The difference between these two functions is that the first function returns the entire result set, the second is a tuple that returns a result set one by one, and returns later when Fetch is returned. 2. Since MySQL's C API functions use many MySQL custom types, there is a problem with the MFC program. Perhaps it is a MySQL header file and stdafx.h is not compatible. The solution is to write a DLL file written with MFC. Obviously the DLL cannot be written in MFC, you need to write with SDK. Appendix, the MYSQL C variable type The following variable type is defined in the mysql library. We need these variables to use MySQL's functions. These variables have been explained in detail, but these explanations are not important for writing code. MySQL structure, start to define a pointer to this structure type, all critical functions of the mysql successive, including initialization, query, end query to use this pointer as a definition of the parameter structure: typedef struct ST_MYSQL {NET NET; / * Communication parameters * / gptr connector_fd; / * ConnectorFd for SSL * / char * host, * user, * passwd, * unix_socket, * server_version, * host_info, * info, * db; unsigned int port, client_flag, server_capabilities; unsigned int protocol_version; unsigned int field_count; unsigned int server_status; unsigned long thread_id; / * Id for connection in server * / my_ulonglong affected_rows; my_ulonglong insert_id; / * id if insert on table with NEXTNR * / my_ulonglong extra_info; / * Used by mysqlshow * / unsigned long packet_length; enum mysql_status status; MYSQL_FIELD * fields; MEM_ROOT field_alloc; my_bool free_me; / * If free in mysql_close * / my_bool reconnect; / * set to 1 if automatic reconnect * / struct st_mysql_options options; char scramble_buff [9]; struct Charset_info_st * charset; un Signed Int Server_Language;} mysql; MYSQL_RES return structure used to represent the query result value, typedef struct st_mysql_res {my_ulonglong row_count; unsigned int field_count, current_field; MYSQL_FIELD * fields; MYSQL_DATA * data; MYSQL_ROWS * data_cursor; MEM_ROOT field_alloc; MYSQL_ROW row; / * If unbuffered read * / MYSQL_ROW current_row; / * buffer to current row * / unsigned long * lengths; / * column lengths of current row * / MYSQL * handle; / * for unbuffered reads * / my_bool eof; / * Used my mysql_fetch_row * /} MYSQL_RES; other data Description of the type and function Reference MYSQL Reference Manual.