To know how to operate the database through the C language, it is necessary to understand the meaning of these several structures, here is simply to say if you want to know more in-depth can be searched online
C API Data type
1, the MySQL structure represents a database connection handle, contains information about the server's connection status, almost all functions are used to him. To connect to MySQL, a MySQL instance must be established and the connection can begin by mysql_init initialization.
2. Mysql_res results represent the returned query results (select,show, etc.). The returned information for the query is also turned into a result set. In the C API, the corresponding is mysql_res, read data from the database, and finally read the data from the Mysql_res。
3, Mysql_rowThis is a type of row data security(Type-safe) 's representation. A string array that is currently implemented as a count byte. (If the field values may contain binary data, you cannot treat these as null-terminated strings, because such values can be obtained by calling the rows that contain empty bytes internally)
mysql_fetch_row()
.
Here is a summary of the functions that the C API can use, see the MySQL manual
Function |
Describe |
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 () |
Change the users and databases on the open connection. |
Mysql_charset_name () |
Returns the name of the default character set used for the connection. |
Mysql_close () |
Close 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 () |
Create a 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 () |
Revoke the database. The function is no longer valued, and the SQL statement is used to drop database instead. |
Mysql_dump_debug_info () |
Have 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 () |
Get 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 version of the protocol 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 a 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 a field that matches a simple regular expression. |
Mysql_list_processes () |
Returns a list of current server threads. |
Mysql_list_tables () |
Returns the name of the table that matches the simple regular expression. |
Mysql_more_results () |
Check to see 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 () |
Set the connection options for mysql_connect (). |
Mysql_ping () |
Check that 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 () |
Given the current character set of the connection, the special characters in the string are escaped for use in SQL statements. |
Mysql_real_query () |
Executes an SQL query that is specified as a count string. |
Mysql_refresh () |
Refreshes or resets the table and the high-speed buffer. |
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 () |
Set options (such as multiple statements) for the connection. |
Mysql_sqlstate () |
Returns the SQLSTATE error code for the previous error. |
Mysql_shutdown () |
Shut 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. |
Here's how you can manipulate the database through the C API.
The following example reads data from a table in a database
#include <stdio.h>
#include <mysql.h>
#include <string.h>
int main ()
{
MySQL MySQL; MySQL Connection
Mysql_res *res; This structure represents a query result set that returns rows
Mysql_row ROW; A representation of the type safety (type-safe) of a row of data
Char *query; Query statements
int t,r;
Mysql_init (&mysql);
if (!mysql_real_connect (&mysql, "localhost", "root", NULL, "test", 0,null,0))
{
printf ("Error Connecting to Database:%s", Mysql_error (&mysql));
}
Else
{
printf ("connected...\n");
}
query= "SELECT * from QQ";
T=mysql_real_query (&mysql,query, (unsigned int) strlen (query));//executes an SQL query that is specified as a count string.
if (t)
{
printf ("Exception occurred when performing display:%s", Mysql_error (&mysql));
}
Res=mysql_store_result (&mysql);//Retrieve the complete result set to the client.
printf ("name \ t School number \ t age \t\n");
while (Row=mysql_fetch_row (res))
{
For (T=0;t<mysql_num_fields (res); t++)
{
printf ("%s\t", row[t]);
}
printf ("\ n");
}
Mysql_free_result (res);//releases the memory used by the result set.
Mysql_close (&mysql);
return 0;
}
Run the following commands and code:
Actually, it's a very simple thing.
C Api--linux Read database contents (MySQL) on C language