The source http://www.leftworld.net/c API code is distributed with MySQL, which is included in the MySQLClient library and allows the C program to access a database. Many customers in MySQL source code are written in c. If you are looking for how to use the C API, look at these client programs. Most other customers APIs (except Java all) use the MySQLClient library to communicate with MySQL servers. This means, for example, you can take advantage of many of the same environment variables used by other clients because they are referenced from the library. For a table of these variables, see an overview of the different mysql programs of 12.1. The customer has a maximum communication buffer size. The initial allocated buffer size (16K bytes) automatically increases to the maximum size (the default maximum is 24m). Because the buffer size is only increased by ensuring demand, simply increase the default maximum limit does not cause more consumed. This size check is primarily a check of errors and communication packages. The communication buffer must be large enough to a separate SQL statement (for the customer-server transmission) and the data returned (transmitted to the server). The communication buffer of each thread is dynamically enlarged to maximum restrictions to process any query or rows. For example, if you contain a BLOB value of large to 16M data, you must have a at least 16m communication buffer restriction (on both servers and clients). The maximum value of the customer is 24m, but the default maximum in the server side is 1M. You can change it by changing the value of the MAX_ALLOWED_PACKET parameter while the server is started. See 10.2.3 Adjusting Server Parameters. The MySQL server reduces each communication buffer to NET_BUFFER_LENGTH bytes after each query. For customers, the size of the buffer associated with a connection is not reduced until the connection is turned off, and the customer memory is reclaimed at this time. If you use a thread, you should use -With-Thread-Safe-Client to compile MySQL C API, which will make the C API thread more secure. You can share the same connection between 2 threads, as long as you do the following: The two threads cannot send queries to MySQL on the same connection at the same connection. In particular, you must guarantee that there is no other thread between a mysql_query () and mysql_store_result () that is using the same connection. Many lines can access different result sets of mysql_store_result (). If you use mysql_use_result, you must guarantee that there is no other thread to ask anything on the same connection until the result set is turned off. 20.2 C API Data Type MySQL This structure represents a handle for a database connection, which is used for almost all MySQL functions. MySQL_RES This structure represents the result of a query of the return line (SELECT, SHOW, DESCRIBE, EXPLAIN). The information returned from the query is hereinafter referred to as the result collection below this chapter. MySQL_ROW This is a representation of the type of tail-safe (Type-Safe). It is currently implemented as a number of bytes of string arrays. (If the field value may contain binary data, you cannot treat these as an air stop string, because such values can be included in the internal block by call mysql_fetch_row () by calling mysql_fetch_row (). MySQL_Field This structure contains field information, such as field name, type, and size. Its members are described in more detail below. You can get the mysql_field structure for each column by repeating mysql_fetch_field (). The field value is not part of this structure; they are included in a mysql_row structure. MySQL_Field_offset This is a type of security of a relative to a mysql field table. (Used by mysql_field_seek ().) The offset is the field number within a row, starting from 0. My_ulonglong This type is used for row number and mysql_affected_rows (), mysql_num_rows () and mysql_insert_id ().
This type provides a range of 0 to 1.84E19. On some systems, trying to print the value of MY_ULONGLONG will not work. In order to print such a value, transform it to the unsigned long and use one% Lu print format. For example: Printf (NUMBER OF ROWS:% lu / n ", (unsigned long) mysql_num_rows (result)); Mysql_field structure contains a member of the following: char * name field name, is a space-ended string. Char * Table Contains the name of the table for this field, if it is not a calculated field. For the calculated field, the table value is an empty string. CHAR * DEF The default value of this field is a space-ended string. As long as you use, only you use mysql_list_fields () before setting it enum enum_field_types .type value field type can be one of the following types: type value type meaning FIELD_TYPE_TINY TINYINT field FIELD_TYPE_SHORT SMALLINT field FIELD_TYPE_LONG INTEGER field FIELD_TYPE_INT24 MEDIUMINT field FIELD_TYPE_LONGLONG BIGINT field FIELD_TYPE_DECIMAL DECIMAL or NUMERIC field FIELD_TYPE_FLOAT FLOAT field FIELD_TYPE_DOUBLE DOUBLE or REAL field FIELD_TYPE_TIMESTAMP TIMESTAMP field FIELD_TYPE_DATE DATE field FIELD_TYPE_TIME TIME field FIELD_TYPE_DATETIME DATETIME FIELD_TYPE_STRING string field FIELD_TYPE_YEAR YEAR field (CHAR or VARCHAR) or TEXT field FIELD_TYPE_BLOB BLOB fields (max_length used to determine the maximum length) FIELD_TYPE_SET SET field FIELD_TYPE_ENUM ENUM Field field_type_null null-Type field field_type_char is not recommended; use field_type_tiny instead of you can use the IS_NUM () macro to test the field has a digital type. Pass the TYPE value to IS_NUM () and if the field is a number, it will calculate: True: IF (IS_NUM (Field-> Type) Printf ("Field IS Numeric / N "); UNSIGNED INTLENGTH field width, specified in the table definition. Unsigned int MAX_LENGTH The maximum width of the field of the result set (the length of the longest field value of the row in the result collection). If you use mysql_store_Result () or mysql_list_fields (), this contains the maximum length of the field. If you use mysql_use_result (), the value of this variable is zero. Different bit flags of the unsigned int flags field. The FLAGS value can be zero or more sub-bit settings: The flag value flag Meaning NOT_NULL_FLAG field cannot be a null pri_key_flag field is a part of the primary key unique_key_flag field is a unique key Multiple_Key_Flag field is a non-only key.
UNSIGNED_FLAG field has UNSIGNED attribute ZEROFILL_FLAG field has ZEROFILL property BINARY_FLAG field has the BINARY attribute AUTO_INCREMENT_FLAG field has AUTO_INCREMENT attribute ENUM_FLAG field is an ENUM (not recommended) BLOB_FLAG field is a BLOB or TEXT (not recommended) TIMESTAMP_FLAG field is a TIMESTAMP (not recommended) BLOB_FLAG The use of Enum_flag and TimeStamp_FLAG flags is not recommended because they point out the type of fields rather than its type properties. It is best to test FIELD-> TYPE for field_type_blob, field_type_timestamp. The following example demonstrates a typical Flags value: if (Field-> Flags & Not_Null_Flag) Printf ("Field CAN't Be Null / N); you can use the following convenient macro to determine the Boolean status of the FLAGS value: IS_NOT_NULL (Flags) really, if the field is defined as NOT NULL IS_PRI_KEY (FLAGS) true, if this field is a primary key is_blob (flash) true, if this field is a blob or text (not recommended; reverse test Field-> TYPE ) UNSIGNED INT Decimals The number of fewer numbers to the digital field. 20.3 C API Functions Overview The functions available in the C API are listed below, and will be described in more detail in the next section. See 20.4 C API function description. MySQL_AFFECTED_ROWS () Returns the number of rows affected by the latest Update, Delete or Insert query. MySQL_Close () Close a server connection. MySQL_Connect () Connect a mysql server. This function is not recommended; replaced with mysql_real_connect (). MySQL_CHANGE_USER () changes the user and database on an open connection. MySQL_CREATE_DB () Creates a database. This function is not recommended; and uses the SQL command CREATE DATABASE. MySQL_DATA_SEEK () Search for a list of query results. MySQL_Debug () makes a DBUG_PUSH with a given string. MySQL_DROP_DB () Abandon a database. This function is not recommended; and use the SQL command DROP DATABASE. MySQL_DUMP_DEBUG_INFO () Let the server write debug information to the log file. MySQL_EOF () determines if a final line of a result collection is already read. This function is opposed; mysql_errno () or mysql_error () can be used in contrast. MySQL_ERRNO () Returns an error number of the most calling MySQL function. MySQL_ERROR () Returns an error message for the Mysql function that is recently called. Mysql_escape_string () uses a special character of the escape of the string in the SQL statement. MySQL_FETCH_FIELD () returns the type of the next table field. MySQL_FETCH_FIELD_DIRECT () returns a type of table field to give a field number. MySQL_FETCH_FIELDS () Returns an array of all field structures. MySQL_FETCH_LENGTHS () Returns the length of all columns in the current row. MySQL_FETCH_ROW () gets the next line from the result collection. Mysql_field_seek () puts the column cursor on a specified column. MySQL_field_count () Returns the number of results columns of the most recently query.
MySQL_Field_Tell () returns the location of the field cursor for the last mysql_fetch_field (). MySQL_Free_Result () Releases the memory used by the result collection. MySQL_GET_CLIENT_INFO () Returns the customer version information. MySQL_GET_HOST_INFO () returns a string that describes the connection. MySQL_GET_PROTO_INFO () Returns the protocol version of the connection used. MySQL_GET_SERVER_INFO () returns the server version number. MySQL_INFO () Returns information about recently executed query. Mysql_init () gets or initializes a MySQL structure. MySQL_INSERT_ID () Returns a previous query to generate an AUTO_INCREMENT column. MySQL_KILL () kills a given thread. MySQL_LIST_DBS () returns a database name that matches a simple regular expression. MySQL_List_fields () returns a column name that matches a simple regular expression. MySQL_LIST_PROCESSESSSESSESSSES () Returns a table of the current server thread. MySQL_LIST_TABLES () returns a table name that matches a simple regular expression. MySQL_NUM_FIELDS () Returns the number of columns of the result collection. MySQL_NUM_ROWS () Returns the number of rows in a result set. MySQL_OPTIONS () Sets the connection option to mysql_connect (). MySQL_PING () Checks if the connection to the server is working, and if necessary, reconnect. MySQL_Query () performs SQL query specified as a space-ended string. MySQL_REAL_CONNECT () Connect a MySQL server. MySQL_Real_Query () Performs the SQL query specified as a string with counts. MySQL_RELOAD () tells the server to reload the authorization form. MySQL_ROW_SEEK () Search in rows in the result collection use the value returned from MySQL_ROW_TELL (). MySQL_ROW_TELL () returns the line of row. MySQL_SELECT_DB () Connect a database. MySQL_SHUTDOWN () Turn off the database server. MySQL_STAT () returns a server status as a string. MySQL_Store_Result () Retrieves a complete result collection to the customer. MySQL_THREAD_ID () Returns the ID of the current thread. MySQL_USE_RESULT () initializes a retrieval of a row of row of results. In order to connect the server, mysql_init () is called to initialize a connection processor and then call mysql_real_connect () with the processor (other information such as host names, user names, and passwords). When you use this connection to complete your work, call mysql_close () to terminate it. When a connection is active, the customer can send the SQL query to the server with mysql_query () or mysql_real_query (). The difference between the two is Mysql_Query () expects to query the string of empty ends to specify and mysql_real_query () expect a string of counting. If the string contains binary data (which can include a null byte), you must use mysql_real_query (). For each non -SELECT query (for example, INSERT, UPDATE, DELETE, you can call mysql_affected_rows () know how many rows are affected (changing). For the SELECT query, you retrieve the selected rows as a result collection. (Note Some statements are class SELECT, they return lines. These include Show, Describe, and Explain. They should treat them like the SELECT statement.) For customers, there are two ways to process results collection. One method is to retrieve all results immediately by calling mysql_store_result ().
This function gets all rows returned from the server and stores them on the client. The second method is to initialize the retrieval of a row of one line of row of row results to customers by calling Mysql_Use_Result (). This function is initialized to retrieve, but it does not actually get any rows from the server. In both cases, you have row via mysql_fetch_row (). Use mysql_store_Result (), mysql_fetch_row () stores the row that has been removed from the server. Use mysql_use_result (), mysql_fetch_row () actually retrieve line from the server. Call mysql_fetch_lengths () to obtain information about data value size per row. After you run a result collection, call mysql_free_result () release the memory it uses. Two search mechanisms are complementary. The client should choose the way to best suggest them. In practice, customers are usually more willing to use mysql_store_result (). One advantage of mysql_store_result () is that since the rows are taken by the customer, you can not only order line, you can also mysql_data_seek () or mysql_row_seek () move forward in front and rear in the result collection to change the current line location in the result collection. You can also know how many rows can be known by calling mysql_num_rows (). On the other hand, mysql_store_result () memory requirements may be high on larger result sets, and you most likely encountered out-of-memory. One advantage of mysql_use_result () is that the customer requires less memory, because it is just a line (and because there is less allocation overhead, mysql_use_result () can be faster). Disadvantages You must handle each row as soon as possible to avoid trapped, you don't have to have a collection of rows in the collection (you can only depart), and you don't know how many lines in the result collection until you have retrieved all result. Also, you have to retrieve all the rows, even if you are in the middle of the way, you have found the information you want to find. The API makes the customer correctly answer the query (only the necessary rows only), do not know if the query is a SELECT. You can call mysql_store_result () by mysql_quence () (or mySQL_REAL_QUERY ()). If the result set call is successful and the query is a SELECT, you can read the row. If the result is called, call mysql_field_count () determine whether the result is actually expected. If mysql_field_count () returns 0, the query has not returned data (indicating that it is an insert, update, delete, etc.), so it is not desirable to return. If mysql_field_count () is non-zero, the query should be returned, but no. This indicates that the query is a failure of SELECT. See how mysql_field_count () can be described. MySQL_STORE_RESULT () and mysql_use_result () all allow you to get information about the fields of forming results (the number of fields, their names, and types, etc.). You can sequentially access field information in the row by repeating the mysql_fetch_field (), or call the field number in the mysql_fetch_field_direct (). The current field cursor position can be changed by calling mysql_field_seek (), set the field cursor to affect the subsequent mysql_fetch_field () call. You can also get field information immediately by calling mysql_fetch_fields (). For testing and reporting errors, MySQL provides an error message access by means of mysql_errno () and mysql_error () functions. They returned the error code or error message that recently called possible success or failure, allowing you to determine when an error occurred and what error occurred.
20.4 C API Function Description In the following description, a NULL parameter or the return value meaning is NULL in the C programming locale, not a MySQL NULL value. Returns a function of a value is usually returned to a pointer or an integer. Unless otherwise specified, the function returns a pointer returns a non-NULL value indicating that success, or a null value indicates an error, and a function returns an integer returns zero means success, or non-zero means an error. Note that "non-zero" only has this meaning. Unless otherwise description, do not test other values other than zero: if (result) / * correct * / ... error ... if (Result <0) / * incorrect * / ... error ... if (Result == -1) / * incorrect * / ... error ... When a function returns an error, the error section of the function describes the error population is listed. You can call mysql_errno () to find out which one has occurred. The wrong string representation can call mysql_error (). 20.4.1 mysql_affected_rows () my_ulonglong mysql_affected_rows (mysql * mysql) 20.4.1.1 Description Returns the number of rows affected by the last Update, Delete or INSERT query. You can call immediately after mysql_query () for update, delete or insert statement. For the SELECT statement, the functionality of mysql_affected_rows () is the same as mysql_num_rows (). MySQL_AFFECTED_ROWS () is currently implemented in a macro. 20.4.1.2 An integer greater than zero represents the number of rows that are affected or retrieved. Zero indicates that there is no record of the WHERE clause in the match, or there is currently no query being executed. -1 indicates that the query returns an error, or for a SELECT query, mysql_affected_rows () is called before calling mysql_store_result (). 20.4.1.3 Error is not. 20.4.1.4 Example mysql_query (& mysql, "UPDATE products SET cost = cost * 1.25 WHERE group = 10"); printf ( "% d products updated", mysql_affected_rows (& mysql)); 20.4.2 mysql_close () void mysql_close (MYSQL * mysql 20.4.2.1 Description Close a previously opened connection. If the handle is automatically assigned by mysql_init () or mysql_connect (), mysql_close () also releases the connection handle directed by MySQL. 20.4.2.2 Return The value is not. 20.4.2.3 Error The cr_commands_out_of_sync command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_SERVER_LOST is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.3 mysql_connect () mysql * mysql_connect (mysql * mysql, const char * host, const char * user, const char * passwd) 20.4.3.1 Description This function is not recommended, and better use MySQL_Real_Connect (). MySQL_Connect () tries to create a connection to a MySQL database engine running in Host.
MySQL_Connect () must be successfully completed before you can perform other API functions, except mysql_get_client_info (). The meaning of the parameter is the same as the corresponding parameters of MySQL_Connect (), and the connection parameters can be NULL. In this case, the C API is automatically assigned memory for the connection structure, and when you call mysql_close (), release it. The disadvantage of this method is that if the connection fails, you cannot retrieve an error message. (To get an error message from MySQL_ERRNO () or MySQL_ERROR (), you must provide a valid mysql pointer.) 20.4.3.2 Return Value with MySQL_Real_Connect (). 20.4.3.3 Error with mySQL_REAL_CONNECT (). 20.4.4mysql_change_user () My_BOOL MYSQL_CHANGE_USER (mysql * mysql, const char * user, const char * password, const char * dB) 20.4.4.1 Description Change the user and make the database specified by the DB be the default to the connection specified by MySQL ( Current) database. In subsequent queries, this database is the default value for table references that do not include a clear database specifier. This function function is introduced in MySQL 3.23.3. Unless the user can be authenticated or if he does not use the database, mysql_change_user () failed. In this case, the user and the database are not changed. If you don't want to have a default database, the DB parameter can be set to NULL. 20.4.4.2 Return The value is successful, zero. If an error occurs, non-zero. 20.4.4.3 Error The same as you can get from MySQL_Real_Connect (). The CR_COMMANDS_OUT_OF_SYNC command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_SERVER_LOST is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. ER_UNKNOWN_COM_ERROR MYSQL Server does not implement this command (may be an old server) er_access_denied_error user or password error. Er_BAD_DB_ERROR database does not exist. Er_DBACCESS_DENIED_ERROR User There is no database access to the database. Er_Wrong_db_name Database Name is too long. 20.4.4.4 Sample IF (Mysql_Change_User (& Mysql, "User", "Password", "new_database")) {fprintf (stderr, "failed to change user. Error:% s / n", mysql_error (& mysql));} 20.4 .5 mysql_create_db () int mysql_create_db (mysql * mysql, const char * dB) 20.4.5.1 Description Creates a database named after the DB parameter. This function is not recommended, and it is best to use mysql_query () to issue a SQL CREATE DATABASE statement. 20.4.5.2 Return Value If the database is successfully created, zero. If an error occurs, non-zero. 20.4.5.3 Error CR_COMMANDS_OUT_OF_SYNC command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_SERVER_LOST is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR.
20.4.5.4 Example if (mysql_create_db (& mysql, "my_database")) {fprintf (stderr, "Failed to create new database Error:% s / n.", Mysql_error (& mysql));} 20.4.6 mysql_data_seek () void mysql_data_seek (MySQL_RES * Result, unsigned long long offset) 20.4.6.1 Description Locate any line in a query result collection. This requires the result set structure contains all the results of the query, so mysql_data_seek () can be used with MySQL_Store_Result (), not mysql_use_result (). The offset should be a value from 0 to MySQL_NUM_ROWS (Result) -1 range. 20.4.6.2 Return value is not. 20.4.6.3 Error No. 20.4.7 mysql_debug () Void mysql_debug (char * debug) 20.4.7.1 Description Use a given string to make a DBUG_PUSH. MySQL_Debug () uses the Fred Fish debug library. To use this function, you must compile the customer library to support debugging. See G.1 Debugger a mysql server and section G.2 debug a MySQL customer. 20.4.7.2 Return value is not. 20.4.7.3 Error No. 20.4.7.4 Example The call shown below enables a trace file in "/TMP/Client.trace" on the client machine: mysql_debug ("D: T: O, / TMP / Client.trace"); 20.4 .8 mysql_drop_db () int mysql_drop_db (mysql * mysql, const char * dB) 20.4.8.1 Description Abandon the database named by the DB parameter. This function is not recommended, and it is best to use mysql_query () to issue a SQL DROP DATABASE statement. 20.4.8.2 Return Value If the database is successfully broken, zero. If an error occurs, non-zero. 20.4.8.3 Error CR_COMMANDS_OUT_OF_SYNC command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_SERVER_LOST is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.8.4 Example if (mysql_drop_db (& mysql, "my_database")) fprintf (stderr, "Failed to drop the database: Error:% s / n", mysql_error (& mysql)); 20.4.9 mysql_dump_debug_info () int mysql_dump_debug_info (MYSQL * MySQL) 20.4.9.1 Description The instruction server writes some debug information to the log file. The connected user must have precess permissions to work. 20.4.9.2 Return Value If the command is successful, zero. If an error occurs, non-zero. 20.4.9.3 Error The cr_commands_out_of_sync command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_server_lost is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.10 mysql_eof () my_bool mysql_eof (mysql_res * result) 20.4.10.1 Description This function is not recommended, and use mysql_errno () or mysql_error ().
MySQL_EOF () determines whether a final line of the result collection has been read. If you get a result collection from a successful MySQL_Store_Result () call, the client has received all the collection with an operation. In this case, returning a NULL from mysql_fetch_row () always means that the end of the result collection is reached, and mysql_eof () is not necessary to call. On the other hand, if you initialize a retrieval of a result set using mysql_use_result (), the set of the collection will be obtained from the server as you repeatedly call mysql_fetch_row (). Because an error may occur in this process, returning a null value from mysql_fetch_row () does not mean that the collection is normal to the tail. In this case, you can use mysql_eof () to determine what happened. If the end of the result set is reached, MySQL_EOF () returns a non-zero value, and if an error occurs, zero is returned. At time, mysql_eof () is prior to the standard mysql error function mysql_errno () and mysql_error (). Because these error functions provide the same information, they use better MySQL_EOF (), which is not recommended now. (In fact, they provide more information because the mysql_eof () value returns a boolean value, and the error function indicates the cause of an error when an error occurs.) 20.4.10.2 Return value If an error occurs, zero. If the end of the result collection is reached, it is non-zero. 20.4.10.3 Error No. 20.4.10.4 Example The following example shows how you must use mysql_eof (): mysql_query (& mysql, "SELECT * FROM some_table"); result = mysql_use_result (& mysql); while ((row = mysql_fetch_row (result))) {// do something WITH DATA} if (! mysql_eof (result)) // mysql_fetch_row () failed due to an error {fprintf (stderr, "error:% s / n", mysql_error (& mysql));}, however, you can use standard mysql errors function to accomplish the same effect: mysql_query (& mysql, "SELECT * FROM some_table"); result = mysql_use_result (& mysql); while ((row = mysql_fetch_row (result))) {// do something with data} if (mysql_errno (& mysql) ) // mysql_fetch_row () failed due to an error {fprintf (stderr, "error:% s / n", mysql_error (& mysql));} 20.4.11 mysql_errno () unsigned int mysql_errno (mysql * mysql) 20.4.11.1 For connections specified by MySQL, MySQL_ERRNO () returns an error code that the most successful or failed API function. The return value is meaningful to happen. The customer error message number is listed in the MySQL "errmsg.h" header file. The server error message number is listed in "MySQLD_ERROR.H". 20.4.11.2 Return Value: An error code value. If there is no error, zero. 20.4.11.3 Error No.
20.4.12 mysql_error () char * mysql_error (mysql * mysql) 20.4.12.1 Description For the connection specified by MySQL, mysql_errno () returns the error code that the most successful or failed API function. If there is no error, return empty string (""). This means that the following two tests are equivalent: if (mysql_errno (& mysql)) {// an error occurred} if (MySQL_ERROR (& mysql) [0]! = '/ 0') {// an error er ccurred} customer The language of the error message can be changed by recompiling the MySQL customer library. Currently, you can select an error message between several different languages. See 9.1 MYSQL support? . 20.4.12.2 Return the value of a string describing the error. If there is no error, an empty string. 20.4.12.3 Error No. 20.4.13 mysql_escape_string () unsigned int mysql_escape_string (char * to, const char * from, unsigned int length) 20.4.13.1 Description SQL encodes a string in from From as a SQL that can be sent to the server in a SQL statement String, place the result in TO and add a termination empty byte. The encoded characters are NUL (ASCII 0), '/ N', '/ R', '/', '' ',' "and Control-Z (see 7.1): How to write strings and numbers). By from The pointing string must be the length of the length. You must assign a buffer to the buffer at least Length * 2 1 byte length. (In a worse case, each character may need to use 2 bytes to encode, And you need a space for terminating empty bytes) When mysql_escape_string () returns, the content of TO will be a string of empty characters. The return value is the length of the encoded string, does not include terminating empty characters. 20.4.13.2 Example Char Query [1000], * end; end = strmov (Query, "Insert INTO TEST_TABLE VALUES ("); * End = '/' '; end = mysql_escape_string (end, "what of them", 11); * end = '/' '; * end =', '; * end =' / '; end = mysql_escape_string (end, "binary data: / 0 / r / n", 16); * end =' / '' ; * end = ')'; if (mysql_real_query ("End - query))) {FPrintf (stderr," failed to insert rot, error:% s / n ", mysql_error (& mysql) The StrMov () function used in the} example is included in the mysqlclient library and functions similar to strcpy (), but returns a pointer to the first parameter of the empty termination. 20.4.13.3 Return value to put the value of TO The length does not include terminating empty characters. 20.4.13.4 Error No.
20.4.14 mysql_fetch_field () mysql_field * mysql_fetch_field (mysql_res * result) 20.4.14.1 Description Returns a column of a column as a result set as a mysql_field structure. Repeating this function retrieves all the information about the column in the result collection. When there is no more fields, mysql_fetch_field () returns NULL. Each time you perform a new SELECT query, MySQL_FETCH_FIELD () is reset (reset) to return information about the first column. The field returned by mysql_fetch_field () is also called the impact of mysql_field_seek (). If you call mysql_query () to execute a Select on a table, but did not call mysql_store_result () if you call mysql_fetch_field () asked the length of a blob field, mysql returns the default BLOB length (8k bytes). (The length of the 8K is because MySQL doesn't know the maximum length of blob. This should be configurable at some time) Once you have retrieved the result collection, Field-> max_length contains the maximum value for the column in the specific query. length. 20.4.14.2 Return the value of the current column of MySQL_Field structure. If there is no column, NULL. 20.4.14.3 Error No. 20.4.14.4 Examples MYSQL_FIELD * field; while ((field = mysql_fetch_field (result))) {printf ( "field name% s / n", field-> name);} 20.4.15 mysql_fetch_fields () MYSQL_FIELD * mysql_fetch_fields (MYSQL_RES * Result 20.4.15.1 Description Returns an array of all mysql_field structures that returns a result set. The field definition of each structure provides a column in combination. 20.4.15.2 Returns an array of all MySQL_Field structures for a result collection. 20.4.15.3 Error No. 20.4.15.4 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 20.4.16.4 Example unsigned int num_fields; unsigned int i; MYSQL_FIELD * field; num_fields = mysql_num_fields (result); for (i = 0; i 20.4.18.3 Error CR_SERVER_LOST Lost on the server connection during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.18.4 Example 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 20.4.19.4 Examples MYSQL_RES * result; unsigned int num_fields; unsigned int num_rows; if (mysql_query (& mysql, query_string)) {// error} else // query succeeded, process any data returned by it {result = mysql_store_result (& mysql); if (result) // there are rows {num_fields = mysql_num_fields (result); // retrieve rows, then call mysql_free_result (result)} else // mysql_store_result () returned nothing; should it have {if (mysql_field_count (& mysql) =? = 0) {// query does not return data // (it was not a SELECT) num_rows = mysql_affected_rows (& mysql);} else // mysql_store_result () should have returned data {fprintf (stderr, "Error:% s / n ", mysql_error (& mysql));}}} Another option is to use mysql_errno (& mysql) to call Mysql_field_count (& mysql). In this case, you directly check an error from mysql_store_result () instead of the inferior statement is a SELECT from the mysql_field_count () value. 20.4.20 mysql_field_seek () mysql_field_offset mysql_field_seek (mysql_res * result, mysql_field_offset offset) 20.4.20.1 Description Setting the field cursor to a given offset. The next call mysql_fetch_field () will retrieve the field definition of the column associated with the offset. In order to be positioned at the start of the row, a OFFset value of 0 is transmitted. 20.4.20.2 Return the previous value of the value field cursor. 20.4.20.3 Error No. 20.4.21MYSQL_FIELD_TELL () mysql_field_offset mysql_field_tell (mysql_res * result) 20.4.21.1 Description Returns the location of the field cursor for the last mysql_fetch_field (). This value can be used as a parameter for mysql_field_seek (). 20.4.21.2 Return the current offset of the value field cursor. 20.4.21.3 Error No. 20.4.22 mysql_free_result () void mysql_free_result (mysql_res * result) 20.4.22.1 Description Release The memory is allocated by mysql_store_result (), mysql_use_result (), mysql_list_dbs (), etc. When you run out of a result collection, you must call mysql_free_result () to release the memory it uses. 20.4.22.2 Return value is not. 20.4.22.3 Error No. 20.4.23 mysql_get_client_info () char * mysql_get_client_info (void) 20.4.23.1 Description Returns a string that represents the version of the client library. 20.4.23.2 Return Value Represents a string of the MySQL customer library version. 20.4.23.3 Error No. 20.4.24 mysql_get_host_info () char * mysql_get_host_info (mysql * mysql) 20.4.24.1 Description Returns a string that describes the connection type being used, including serving its hostname. 20.4.24.2 Return Value Represents a string of the server hostname and connection type. 20.4.24.3 Error No. 20.4.25 mysql_get_proto_info () unsigned int mysql_get_proto_info (mysql * mysql) 20.4.25.1 Description Returns the protocol version used to use. 20.4.25.2 Return Value Indicates an unsigned integer of the protocol version used by the current connection. 20.4.25.3 Error No. 20.4.26 mysql_get_server_info () char * mysql_get_server_info (mysql * mysql) 20.4.26.1 Description Returns a string representing the server version number. 20.4.26.2 Return Value Represents a string of the server version number. 20.4.26.3 Error No. 20.4.27 mysql_info () char * mysql_info (mysql * mysql) 20.4.27.1 Description Retrieves a string that provides information about the recently executed query, but the statement listed below. Returns NULL to other statements. The format of the string varies with the query type, as described below. The figures are merely illustrative; the string will contain the appropriate values for the query. INSERT INTO ... SELECT ... String format: Records: 100 duplicates: 0 Warnings: 0 Insert Into ... Values (...), (...), (...) ... String Format: Records: 3 Duplicates: 0 Warnings: 0 load data infile ... string format: Records: 1 deleted: 0 Skipped: 0 Warnings: 0 Alter Table string rope format: Records: 3 duplicates: 0 Warnings: 0 Update String: Rows Match: 40 Changed: 40 Warnings: 0 Note that only multiple values are specified in the statement, MySQL_INFO () returns a non-null value to the INSERT ... VALUES statement. 20.4.27.2 Return Value Represents a string of additional information for the recently executed query. If you don't get any information for the query, NULL. 20.4.27.3 Error No. 20.4.4.28 mysql_init () mysql * mysql_init (mysql * mysql) 20.4.28.1 Description Distribution or initialize a MySQL object that is suitable for mysql_real_connect (). If mysql is a NULL pointer, a function assignment, initialization, and returns a new object. Otherwise, the object is initialized and returned to the address of the object. If mysql_init () assigns a new object, it will be released when calling mysql_close () to turn off the connection. 20.4.28.2 Return to a value of an initialized mysql * handle. If there is not enough memory to assign a new object, NULL. 20.4.28.3 Error Returns NULL in the case of insufficient memory. 20.4.29 mysql_insert_ID () my_ulonglong mysql_insert_id (mysql * mysql) 20.4.29.1 Description Returns the ID that is generated by the previous query as an auto_increment column. Use this function after inserting an Insert query to a table containing the auto_increment field. Note that if the previous query does not generate an auto_increment value, MySQL_INSERT_ID () returns 0. If you need to save this value in the future, you must call mysql_insert_id () immediately after the query is generated. Also note that the Last_Insert_ID () function of SQL always contains the most recently generated auto_increment value and is not reset between the query because the value of the function is maintained at the server. 20.4.29.2 Return Value has the value of the auto_increment field of the previous query update. Returns zero if there is no previous inquiry on the connection or if the query is not updated the auto_increment value. 20.4.29.3 Error No. 20.4.30 mysql_kill () int mysql_kill (mysql * mysql, unsigned long pid) 20.4.30.1 Description Requires the server to kill the thread specified by the PID. 20.4.30.2 The return value is successful, zero. If an error occurs, non-zero. 20.4.30.3 Error CR_COMMAVANDS_OUT_OF_SYNC command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_server_lost is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.31 mysql_list_dbs () mysql_res * mysql_list_dbs (mysql * mysql, const char * wild) 20.4.31.1 Description Returns a result set, which consists of a simple regular expression of the simple regular expression specified on the server. Wild can contain wildcard characters "%" or "_", or may be a NULL pointer to match all databases. Call mysql_list_dbs () is similar to the execution query show databases [Like Wild]. You must release the result collection with mysql_free_result (). 20.4.31.2 Return value is successful, a mysql_res result collection. If an error occurs, NULL. 20.4.31.3 Error Cr_commands_out_of_sync command is performed in an incorrect order. CR_out_of_memory memory overflow. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_server_lost is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.32 mysql_list_fields () mysql_res * mysql_list_fields (mysql * mysql, const char * table, const char * wild) 20.4.32.1 Description Returns a result set, which is used in a simple regular expression specified in a given Wild parameter The column name consists. Wild can contain wildcard characters "%" or "_", or may be a NULL pointer that matches all columns. Call mysql_list_fields () is similar to the execution query show colors from TBL_NAME [LIKE WILD]. Note that you recommend that you use Show Column from TBL_NAME instead of mysql_list_fields (). You must release the result collection with mysql_free_result (). 20.4.32.2 The return value is successful, and a mesql_RES result collection. If an error is out, NULL. 20.4.32.3 Error The cr_commands_out_of_sync command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_server_lost is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.33 mysql_list_processes () mysql_res * mysql_list_processes (mysql * mysql) 20.4.33.1 Description Returns a collection of results that describe the current server thread. This is the same information as the MySQLADMIN ProcessList or Show ProcessList query report. You must release the result collection with mysql_free_result (). 20.4.33.2 The return value is successful, and a mysql_res result collection. If an error occurs, NULL. 20.4.33.3 Error CR_COMMANDS_OUT_OF_SYNC command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_server_lost is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.34 mysql_list_tables () mysql_res * mysql_list_tables (mysql * mysql, const char * wild) 20.4.34.1 Description Returns a collection of results, which consists of the table name of the simple regular expression specified in the current database. Wild can include wildcard characters "%" or "_", or may be a NULL pointer to match all tables. Call mysql_list_tables () Similar to the execution query show Tables [Like Wild]. You must release the result collection with mysql_free_result (). 20.4.34.2 The return value is successful, and a mysql_res result collection. If an error occurs, NULL. 20.4.34.3 Error Cr_commands_out_of_sync command is performed in an incorrect order. The CR_SERVER_GONE_ERROR MYSQL server is turned off. CR_server_lost is lost during the query. A unknown error occurred in CR_UNKNOWN_ERROR. 20.4.35 mysql_num_fields () unsigned int mysql_num_fields (mysql_res * result) or unsigned int mysql_num_fields (mysql * mysql) second in the mysql 3.22.24 or updated version cannot work. In order to pass a mysql * parameter, you must use unsigned int mysql_field_count (mysql * mysql). 20.4.35.1 Description Returns the number of columns in the result collection. Note that you can also get the number of columns by a pointer to a result set or a connecting handle. If mysql_store_result () or mysql_user_result () returns NULL, you will use the connection handle (so you don't have a result set pointer). In this case, you can call mysql_field_count () to determine if mysql_store_result () should generate non-empty results. This allows the client to take the correct action, do not have to know if the query is a SELECT (or class SELECT) statement. The example shown below shows how this can be done. See 20.4.51 Why after mysql_Query () returns successfully, mysql_store_result () sometimes returns NULL? 20.4.35.2 Return Value Represents an unsigned integer in the number of fields in a result set. 20.4.35.3 Error No. 20.4.35.4 Examples MYSQL_RES * result; unsigned int num_fields; unsigned int num_rows; if (mysql_query (& mysql, query_string)) {// error} else // query succeeded, process any data returned by it {result = mysql_store_result (& mysql); if (result) // there are rows {num_fields = mysql_num_fields (result); // retrieve rows, then call mysql_free_result (result)} else // mysql_store_result () returned nothing; should it have {if (mysql_errno (& mysql))? {FPRINTF (stderr, "error:% s / n", mysql_error (& mysql));} else if (mysql_field_count (& mysql) == 0) {// query does not return data // (IT WAS NOT A SELECT) NUM_ROWS = mysql_affected_rows (& mysql);}}} Another option (if you know that your query should return a result) is a check with mysql_field_count (& mysql) = 0 instead of mysql_errno (& mysql). This will only happen in an error. 20.4.36 mysql_num_rows () my_ulonglong mysql_num_rows (mysql_res * result) 20.4.36.1 Description Number of rows returned in the result collection. The use of mysql_num_rows () depends on whether you use mysql_store_Result () or mysql_use_result () to return a result collection. If you use mysql_store_Result (), mysql_num_rows () can be called immediately. If you use MySQL_USE_RESULT (), mysql_num_rows () will not return the correct value until all rows in the result collection are retrieved. 20.4.36.2 Return Value The number of banks in the result collection. 20.4.36.3 Error No. 20.4.37 mysql_options () int mysql_options (mysql * mysql, enum mysql_option option, const char * arg) 20.4.37.1 Description can be used to set an additional connection option and affect a connection. This function can be called multiple options to set multiple options. MySQL_OPTIONS () should be called before mysql_init () and mysql_connect () or mysql_real_connect (). The Option parameter is the option you want to set; the arg parameter is the value of the option. If the option is an integer, then ARG should point to the integer value. Possible option values: Option parameter type function mysql_opt_connect_timeout unsigned int * Take the connection timeout in seconds. MySQL_OPT_CompRESS does not use the compressed client / server protocol. MySQL_OPT_NAMED_PIPE does not use the named pipe connected to a MySQL server on NT.