MySQL study note _ C ++/C in 13_Linux connect to MySQL database (3) -- Process returned data

Source: Internet
Author: User

Connect C ++/C to MySQL database in Linux (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, the other is similar, # include <iostream> # include <fstream> # include <cstdlib> # include <mysql/mysql. h> 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 Serve R 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 (connectio N, 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 */} MYSQL_FIELD; 3. IS_NUM macro. If the field type is numeric, true is returned. If (IS_NUM (mysql_field_ptr-> type) {cout <"Number" <endl;} 4. MYSQL_FIELD_OFFSET mysql_field_seek (MYSQL_RES * result, MYSQL_FIELD_OFFSET offset ); // The function sets the field cursor to the given offset. The next call of mysql_fetch_field will retrieve the field definitions of the columns associated with the offset. If the key locates the beginning of the row, an offset value with a value of 0 will be passed.

Example:

# Include <iostream> # include <fstream> # include <cstdlib> # include <mysql/mysql. h> using namespace std; void mysql_err_function (MYSQL * connection); // the preceding example shows void mysql_display (MYSQL * mysql, MYSQL_ROW sqlrow ); // for implementation, see the preceding example void mysql_display_head (MYSQL_RES * my_res); int main () {MYSQL * connection; connection = mysql_init (NULL); if (mysql_real_connect (connection, "localhost ", "root", "123456", "test", 0, NULL, 0) {co Ut <"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); if (my_res) {mysql_display_head (my_res); MYSQL_ROW sqlrow; cout <"Column Details:" <endl; 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_display_head (MYSQL_RES * my_res) {MYSQL_FIELD * my_field; cout <" Column Describe: "<endl; while (my_field = mysql_fetch_field (my_res) {cout <"\ tName:" <my_field-> name <endl; cout <"\ tType :"; if (IS_NUM (my_field-> type) {cout <"Numeric field";} else {switch (my_field-> type) {case FIELD_TYPE_VAR_STRING: cout <"Varchar "; break; case FIELD_TYPE_LONG: cout <"Long"; break; default: cout <"is" <my_field-> type <", check in mysql. h "; break ;}}cout <endl; cout <" \ tMax width: "<my_field-> length <endl; if (my_field-> flags & AUTO_INCREMENT_FLAG) {cout <"\ tAUTO_INCREMENT" <endl ;}cout <endl ;}}

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.