MySQL C API

Source: Internet
Author: User

The functions that the C API can use are summarized here, and they are described in detail in the next section.
Function description
Mysql_affected_rows () returns the number of rows that were changed/deleted/inserted by the last update, delete, or insert query.
Mysql_autocommit () Toggle autocommit mode, ON/off
Mysql_change_user () Changes the user and database on the open connection.
Mysql_charset_name () returns the name of the default character set used for the connection.
Mysql_close () closes the server connection.
Mysql_commit () commits the transaction.
Mysql_connect () Connect to the MySQL server. The function is no longer valued and is replaced with Mysql_real_connect ().
mysql_create_db () to create the database. The function is no longer valued, use the SQL statement CREATE database instead.
Mysql_data_seek () finds the attribute row number in the query result set.
Mysql_debug () executes the Dbug_push with the given string.
mysql_drop_db () revokes the database. The function is no longer valued, and the SQL statement is used to drop database instead.
Mysql_dump_debug_info () lets the server write debug information to the log.
Mysql_eof () determines whether the last row of the result set is read. The function is no longer valued and can be replaced with Mysql_errno () or mysql_error ().
Mysql_errno () returns the error number of the last called MySQL function.
Mysql_error () returns the error message for the last call to the MySQL function.
Mysql_escape_string () in order to be used in SQL statements, special characters are escaped.
Mysql_fetch_field () returns the type of the next table field.
Mysql_fetch_field_direct () returns the type of the table field given the number of fields.
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 row from the result set
Mysql_field_seek () places the column cursor in the specified column.
Mysql_field_count () returns the number of result columns for the last executed statement.
Mysql_field_tell () returns the position of the field cursor used by the last Mysql_fetch_field ().
Mysql_free_result () frees the memory used by the result set. Mysql_get_client_info () Returns the client version information as a string.
Mysql_get_client_version () Returns the client version information as an integer.
Mysql_get_host_info () returns a string that describes the connection.
Mysql_get_server_version () returns the version number of the server as an integer.
Mysql_get_proto_info () Returns the protocol version used by the connection.
Mysql_get_server_info () returns the version number of the server.
Mysql_info () returns information about the most recently executed query.
Mysql_init () Gets or initializes the MySQL structure.
MYSQL_INSERT_ID () returns the ID of the previous query that was generated for the Auto_increment column.
Mysql_kill () kills the given thread.
Mysql_library_end () finalize the MySQL C API library.
Mysql_library_init () initializes the MySQL C API library.
Mysql_list_dbs () returns the name of the database that matches the simple regular expression.
Mysql_list_fields () returns the name of the field that matches the simple regular expression.
Mysql_list_processes () Returns a list of the current server threads.
Mysql_list_tables () returns the name of the table that matches the simple regular expression.
Mysql_more_results () Checks if there are other results.
Mysql_next_result () Returns/initializes the next result during multi-statement execution.
Mysql_num_fields () returns the number of columns in the result set.
Mysql_num_rows () returns the number of rows in the result set.
Mysql_options () Sets the connection options for mysql_connect ().
Mysql_ping () Check if the connection to the server is working and reconnect if necessary.
Mysql_query () executes a SQL query that is specified as a null-terminated string.
Mysql_real_connect () Connect to the MySQL server.
Mysql_real_escape_string () takes into account the current character set of the connection and, in order to be used in the SQL statement, escapes the special characters in the string.
Mysql_real_query () executes a SQL query that is specified as a count string.
Mysql_refresh () Refreshes or resets the table and high-speed buffering.
Mysql_reload () Notifies the server to load the authorization table again. Mysql_rollback () rolls back the transaction.
Mysql_row_seek () finds the row offset in the result set using the value returned from Mysql_row_tell ().
Mysql_row_tell () returns the row cursor position.
mysql_select_db () Select the database.
Mysql_server_end () finalize the embedded server library.
Mysql_server_init () initializes the embedded server library.
Mysql_set_server_option () Sets options (such as multiple statements) for the connection.
Mysql_sqlstate () returns the SQLSTATE error code for the previous error.
Mysql_shutdown () shuts down the database server.
Mysql_stat () returns the server state as a string.
Mysql_store_result () Retrieves the complete result set to the client.
MYSQL_THREAD_ID () returns the current thread ID.
Mysql_thread_safe () returns 1 if the client has been compiled for thread-safe.
Mysql_use_result () Initializes a row-wise result set retrieval.
Mysql_warning_count () returns the number of alarms for the previous SQL statement.
  1. Initialize the MySQL library by calling Mysql_library_init (). The library can be either the Mysqlclient C client library or the Mysqld Embedded server library, depending on whether the application is linked to the "-libmysqlclient" or "-LIBMYSQLD" flag.  2. The connection handler is initialized by calling Mysql_init () and connected to the server by calling Mysql_real_connect ().  3. Emits an SQL statement and processes its results. (in the following discussion, the method of using it is described in detail).  4. Close the connection to the MySQL server by calling Mysql_close ().  5. End the use of the MySQL library by calling Mysql_library_end ().   Calls Mysql_library_init () and Mysql_library_end () to provide proper initialization and end processing for the MySQL library. For applications that are linked to the client library, they provide improved memory management capabilities. If you do not call Mysql_library_end (), the memory block will remain allocated (this will not increase the amount of memory used by the application, but some memories leak detectors will protest it). For applications that are linked to an embedded server, these calls start and stop the server.  mysql_library_init () and mysql_library_end () are actually the # define symbols, which make them equivalent to Mysql_server_init () and Mysql_server_end (), But its name more clearly indicates that it should be called when the MySQL library is started or ended, regardless of whether the application is using the Mysqlclient or mysqld libraries. For earlier versions of MySQL, you can call Mysql_server_init () and Mysql_server_end () instead.   If you want, you can omit the call to Mysql_library_init () because, if necessary, Mysql_init () automatically calls it.   to connect to the server, call Mysql_init () to initialize the connection handler, and then call Mysql_real_connect () with the handler (and other information, such as host name, user name, and password). After the connection is established, in the API below 5.0.3, Mysql_real_connect () sets the re-connect flag (part of the MySQL structure)to 1, or in a newer version, set it to 0. For this flag, the value "1" indicates that if the statement cannot be executed because of a loss of connection, it will attempt to connect to the server again before discarding. Starting with MySQL 5.0.13, you can use the Mysql_opt_reconnect option on mysql_options () to control the reconnection behavior. When the connection is complete, call Mysql_close () to abort it.   When the connection is active, the client may use mysql_query () or mysql_real_query () to issue a SQL query to the server. The difference is that mysql_query () expects the query to be the specified, null-terminated string, whereas Mysql_real_query () expects a count string. If the string contains binary data (which may contain null bytes), you must use Mysql_real_query ().   For each non-select query (for example, insert, UPDATE, DELETE), by calling Mysql_affected_rows (), you can see how many rows have been altered (affected).   for select queries, the ability to retrieve rows as result sets. Note that some statements are similar to select because they return rows. Includes show, describe and explain. They should be treated in the same way as the SELECT statement. The   client handles the result set in two ways. One way is to retrieve the entire result set at once by calling Mysql_store_result (). The function can get all the rows returned by the query from the server and save them on the client. The second approach is for the client, by calling Mysql_use_result (), to initialize the "by-row" result set retrieval. The function can initialize the results of the search, but it cannot get any actual rows from the server.   In both cases, the row can be accessed by calling Mysql_fetch_row (). With Mysql_store_result (), mysql_fetch_row () can access rows that were previously obtained from the server. With Mysql_use_result (), mysql_fetch_row () is able to actually retrieve rows from the server. by calling Mysql_fetch_lengths (), you can get information about the size of the data in each row.   After you complete the result set operation, call Mysql_free_result () to release the memory used by the result set.   These two search mechanisms are complementary to each other. The client program should choose the method that best meets its requirements.In fact, Mysql_store_result () is most commonly used by clients. The 1 advantage of  mysql_store_result () is that you can not only continuously access the rows, but also use Mysql_data_seek () or Mysql_row_seek () to move forward or backward in the result set because the rows are all fetched to the client. To change the position of the current row within the result set. by calling Mysql_num_rows (), you can also see how many rows are there. On the other hand, for large result sets, the memory required for Mysql_store_result () can be large and you are likely to experience a memory overflow condition. The 1 advantage of  mysql_use_result () is that the client requires less memory for the result set because it maintains only one row at a time (Mysql_use_result () can be faster because of lower allocation overhead). The downside is that you have to deal with each line quickly to avoid interfering with the server, you can't randomly access rows in the result set (you can only access rows consecutively), and you don't know how many rows are in the result set until you retrieve them all. Not only that, you must also retrieve all the rows, even if you decide that you have found the information you are looking for during the retrieval process.   Through the API, the client can respond appropriately to the query (retrieving rows only if necessary) without knowing whether the query is a select query. The operation can be done by calling Mysql_store_result () after each mysql_query () or mysql_real_query (). If the result set is called successfully, the query is select and the row can be read. If the result set call fails, you can call Mysql_field_count () to determine if the result is indeed expected. If Mysql_field_count () returns 0, the query does not return data (indicating that it is an insert, UPDATE, delete, and so on) and does not return rows. If Mysql_field_count () is a non-0 value, the query should return rows, but no rows are returned. This indicates that the query was a failed select. See an Introduction to Mysql_field_count () for an example of how to do this.  , whether Mysql_store_result () or Mysql_use_result (), allows you to get information about the fields that comprise the result set (number of fields, their names and types, and so on). by repeatedly calling Mysql_fetch_field (), you can sequentially access the field information within the row, or by calling Mysql_fetch_field_direct (), to be able to press the field within a rowThe number accesses the field information. by calling Mysql_field_seek (), you can change the cursor position of the current field. The setting of the field cursor will affect subsequent mysql_fetch_field () calls. In addition, you can get all the information about a field at once by calling Mysql_fetch_fields ().   to detect and communicate errors, MySQL provides a mechanism to access error information using the Mysql_errno () and mysql_error () functions. They can return error codes or error messages about recently called functions, and recently called functions may or may not succeed, so you can tell when the error occurred and what the error was.

MySQL C API

Related Article

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.