C api-read MySQL database content in C language on Linux

Source: Internet
Author: User
Tags mysql manual

C api-read MySQL database content in C language on Linux

If you want to know how to operate databases in C language, you must understand the meanings of these structures. Here is a brief introduction. If you want to learn more deeply, you can search for them on the Internet.

C api data type

1. the MySQL structure represents a database connection handle, which contains information about the connection status of the server. Almost all functions use it. To connect to MySQL, you must create a MySQL instance and initiate the connection through MySQL_init.

2. The MySQL_RES result indicates the returned query result (select, show, etc ). The returned information is also the result set ". In the c API, MySQL_RES is used to read data from the database and MySQL_RES.

3. MySQL_ROW is a type-safe representation of row data. Currently, it is implemented as a string array that counts bytes. (If the field value may contain binary data, you cannot regard it as a null termination string because such a value can contain null bytes internally) rows are obtained by calling MySQL_fetch_row.

The functions available for c api are summarized here. For details, see the MySQL manual.

Function

Description

Mysql_affected_rows ()

Returns the number of rows modified, deleted, or inserted by the last UPDATE, DELETE, or INSERT query.

Mysql_autocommit ()

Switch autocommit mode, ON/OFF

Mysql_change_user ()

Change the user and database on the connection.

Mysql_charset_name ()

Returns the name of the default character set for connection.

Mysql_close ()

Disable the server connection.

Mysql_commit ()

Commit a transaction.

Mysql_connect ()

Connect to the MySQL server. This function is no longer valued and replaced by mysql_real_connect.

Mysql_create_db ()

Create a database. This function is no longer valued and replaced by the SQL statement CREATE DATABASE.

Mysql_data_seek ()

Search for the row number in the query result set.

Mysql_debug ()

Execute DBUG_PUSH with the given string.

Mysql_drop_db ()

Undo the database. This function is no longer valued. Instead, use the SQL statement DROP DATABASE.

Mysql_dump_debug_info ()

Allows the server to write debugging information to logs.

Mysql_eof ()

Determine whether the last row of the result set is read. This function is no longer valued. you can replace it with mysql_errno () or mysql_error.

Mysql_errno ()

Returns the error number of the MySQL function called last time.

Mysql_error ()

Returns the error message of the MySQL function called last time.

Mysql_escape_string ()

Escape special characters for use in SQL statements.

Mysql_fetch_field ()

Returns the type of the next table field.

Mysql_fetch_field_direct ()

If the field number is specified, the type of the table field is returned.

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 ()

Obtain the next row from the result set

Mysql_field_seek ()

Place the column cursor in the specified column.

Mysql_field_count ()

Returns the number of result columns of the last statement executed.

Mysql_field_tell ()

Returns the cursor position of the field used in mysql_fetch_field.

Mysql_free_result ()

Releases the memory used by the result set.

Mysql_get_client_info ()

Returns the Client Version in string format.

Mysql_get_client_version ()

Returns the Client Version Information in integer format.

Mysql_get_host_info ()

Returns the string that describes the connection.

Mysql_get_server_version ()

Returns the server version number in integer format.

Mysql_get_proto_info ()

Returns the Protocol Version Used for the connection.

Mysql_get_server_info ()

Returns the server version number.

Mysql_info ()

Returns information about the latest query.

Mysql_init ()

Obtain or initialize the MYSQL structure.

Mysql_insert_id ()

Returns the ID generated in the previous query for the AUTO_INCREMENT column.

Mysql_kill ()

Kill the specified thread.

Mysql_library_end ()

Finally, determine the MySQL c api library.

Mysql_library_init ()

Initialize 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 the list of current server threads.

Mysql_list_tables ()

Returns the name of the table that matches the simple regular expression.

Mysql_more_results ()

Check whether other results exist.

Mysql_next_result ()

Return/initialize the next result during multi-statement execution.

Mysql_num_fields ()

Number of columns in the returned result set.

Mysql_num_rows ()

The number of rows in the returned result set.

Mysql_options ()

Set connection options for mysql_connect.

Mysql_ping ()

Check whether the connection to the server works and try again if necessary.

Mysql_query ()

Execute an SQL query that is specified as a string ending with Null.

Mysql_real_connect ()

Connect to the MySQL server.

Mysql_real_escape_string ()

Considering the connected Current Character Set, escape special characters in the string to be used in SQL statements.

Mysql_real_query ()

Execute the SQL query specified as the Count string.

Mysql_refresh ()

Refresh or reset the table and cache.

Mysql_reload ()

Notifies the server to load the authorization table again.

Mysql_rollback ()

Roll back the transaction.

Mysql_row_seek ()

Use the value returned from mysql_row_tell () to find the row offset in the result set.

Mysql_row_tell ()

Returns the cursor position of a row.

Mysql_select_db ()

Select a database.

Mysql_server_end ()

Finally, the Embedded Server library is determined.

Mysql_server_init ()

Initialize the Embedded Server library.

Mysql_set_server_option ()

Set options for connection (for example, multiple statements ).

Mysql_sqlstate ()

Returns the SQLSTATE error code about the previous error.

Mysql_shutdown ()

Shut down the database server.

Mysql_stat ()

Returns the server status in string format.

Mysql_store_result ()

Retrieve the complete result set to the client.

Mysql_thread_id ()

Returns the ID of the current thread.

Mysql_thread_safe ()

If the client has been compiled into thread-safe, 1 is returned.

Mysql_use_result ()

Initialize row-by-row result set retrieval.

Mysql_warning_count ()

Returns the number of alerts for the previous SQL statement.

Next, let's take a look at how to use c api to operate databases.

 


The following example shows how to read data from a database table.

# Include <stdio. h>
# Include <mysql. h>
# Include <string. h>
Int main ()
{
MYSQL mysql; // mysql connection
MYSQL_RES * res; // This structure indicates a query result set of the returned row.
MYSQL_ROW row; // type-safe representation of the data in a row
Char * query; // query statement
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); // execute an SQL query specified as a Count string.
If (t)
{
Printf ("exception occurred during display: % s", mysql_error (& mysql ));
}
Res = mysql_store_result (& mysql); // retrieve the complete result set to the client.
Printf ("Name \ t student ID \ 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); // release the memory used by the result set.
Mysql_close (& mysql );
Return 0;
}

Run the following command and code:

In fact, it is very simple.

-------------------------------------- Split line --------------------------------------

Install MySQL in Ubuntu 14.04

MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF

Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL

Build a MySQL Master/Slave server in Ubuntu 14.04

Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS

Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04

MySQL-5.5.38 universal binary Installation

-------------------------------------- Split line --------------------------------------

This article permanently updates the link address:

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.