MySQL study note _ 12_Linux C ++/C connect to MySQL database (2) -- return the data SQL

Source: Internet
Author: User

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 );}}

 


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.