MySQL study NOTE _ connecting C ++ C to MySQL database in 13_Linux (3) -- processing returned data Linux study notes
BitsCN. comLinux C ++/C connect to MySQL database (3) -- process returned data 1. number of fields in the returned result set
Unsigned int mysql_field_count (MYSQL * connection); // use the value of MYSQL_ROW as an array that stores a row of data...
Example:
// When one value is obtained at a time, and the other is similar, # include
# Include
# Include
# Include
Using namespace std; void mysql_err_function (MYSQL * connection); void mysql_display (MYSQL * mysql, MYSQL_ROW sqlrow); int main () {MYSQL * connection; connection = mysql_init (NULL ); if (mysql_real_connect (connection, "localhost", "root", "123456", "test", 0, NULL, 0 )) {cout <"Connection to MySQL Server is Succeed... "<endl; string query =" select * from tmp15 "; // getline (cin, query); int res = mysql_query (Connection, query. c_str (); if (res) {mysql_err_function (connection);} else {MYSQL_RES * my_res = mysql_use_result (connection ); // change mysql_use_result to mysql_store_result to get the result of another situation (actually the same ...) if (my_res) {MYSQL_ROW sqlrow; while (sqlrow = mysql_fetch_row (my_res) {mysql_display (connection, sqlrow);} mysql_free_result (my_res );} else {mysql_err_function (connection) ;}} mysql_close (connection ); Cout <"Connection to MySQL Server is Closed! "<Endl ;}else {mysql_err_function (connection) ;}} void mysql_err_function (MYSQL * connection) {if (mysql_errno (connection )) {cout <"Error" <mysql_errno (connection) <":" <mysql_error (connection) <endl; exit (-1 );}} void mysql_display (MYSQL * mysql, MYSQL_ROW sqlrow) {for (unsigned int I = 0; I <mysql_field_count (mysql); ++ I) {printf ("% s ", sqlrow [I]); // cout <sqlrow [I] <''; // if you change printf to cout, an error occurs when you print the value... think about ing ...} cout <endl ;}
2. obtain the information of a field
1. MYSQL_FIELD * mysql_fetch_field (MYSQL_RES * result); 2. MYSQL_FIELD definition: typedef struct st_mysql_field {char * name;/* Name of column */char * table; /* Table of column if column was a field */char * org_table;/* Org table name if table was an alias */char * db; /* Database for table */char * def;/* Default value (set by mysql_list_fields) */unsigned long length;/* Width of column */unsigned long max_length; /* Max width of selected set */unsigned int flags;/* Div flags */unsigned int decimals;/* Number of decimals in field */enum enum_field_types type; /* Type of field. se mysql_com.h for types *