MySQL study note _ 12_Linux C ++/C connect to MySQL database (2) -- return the data SQL
Zookeeper
Connect C ++/C to the MySQL database in Linux (2) -- SQL statement for returning data reference: SQL statement for returning data refers to extracting data records that meet the conditions from the database using a query statement.
There are four steps to retrieve data from the MySQL database value function:
1) issue a query
2) retrieve data
3) process data
4) sort out the required data
Use mysql_query () to issue a query. You can use mysql_store_result () or mysql_use_result () to retrieve data. The next step is to call mysql_fetch_row () to process the data. Finally, you must also call mysql_free_result () to allow MySQL to perform necessary sorting.
1. Extract all data at a time. MYSQL_RES * mysql_store_result (MYSQL * connection); // The structure pointer is returned successfully; otherwise, the NULLmy_ulonglong mysql_num_row (MYSQL_RES * result) is returned ); // slow down the actual number of returned rows MYSQL_ROW mysql_fetch_row (MYSQL_RES * result); // returns the struct of the result from mysql_store_result () and retrieves a single row from it. If no more data exists, or if an error occurs, return NULLvoid mysql_free_result (MYSQL_RES * result); // sort the allocated objects in the mySQL database and close the connection.
# Include # includeusing namespace std; void mysql_err_function (MYSQL * connection); int main () {MYSQL * connection; connection = mysql_init (NULL); if (! Connection) {mysql_err_function (connection);} connection = mysql_real_connect (connection, "localhost", "root", "123456", "test", 0, NULL, 0 ); if (! Connection) {mysql_err_function (connection) ;}cout <"Connection to MySQL Server is Success... "<endl; string query; getline (cin, query); int res = mysql_query (connection, query. c_str (); if (res) {mysql_err_function (connection);} MYSQL_RES * my_res = mysql_store_result (connection); cout <"Retrieved" <mysql_num_rows (my_res) <"rows" <endl; MYSQL_ROW sqlrow; while (sqlrow = mysql_fetch_row (my_re S) {cout <"Fetched data... "<endl;} mysql_free_result (my_res); mysql_close (connection); cout <" Connection to MySQL Server is closed! "<Endl; return 0;} void mysql_err_function (MYSQL * connection) {if (mysql_errno (connection) {cout <" Error "<mysql_errno (connection) <":" <mysql_error (connection) <endl; exit (-1) ;}} [cpp] view plaincopyprint? View the CODE piece on the CODE and derive it to my CODE piece MYSQL_RES * mysql_use_result (MYSQL * connection); // The result set is returned successfully. If it fails, NULL [cpp] view plaincopyprint is returned? View the CODE piece derived from my CODE piece # include using namespace std; void mysql_err_function (MYSQL * connection); 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; getline (cin, query); int res = mysql_query (connection, query. c_str (); if (res ){ Mysql_err_function (connection); // For the implementation code of mysql_err_function (), see the previous example.} else {MYSQL_RES * my_res = mysql_use_result (connection); if (my_res) {MYSQL_ROW sqlrow; while (sqlrow = mysql_fetch_row (my_res) {cout <"Fetching the Data... "<endl;} 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 );}}