MySQL Study Notes _ Connect C ++/C to MySQL database in 12_Linux (2), _ 12_linuxmysql

Source: Internet
Author: User

MySQL Study Notes _ Connect C ++/C to MySQL database in 12_Linux (2), _ 12_linuxmysql
 C ++/C in Linux connects to the MySQL database (2) -- return data SQL reference:

The SQL statement that returns data is used to retrieve data records that meet the conditions from the database.

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 [Cpp]View plaincopyprint?
  1. MYSQL_RES * mysql_store_result (MYSQL * connection );
  2. // Returns the struct pointer. If the pointer fails, NULL is returned.
  3. My_ulonglong mysql_num_row (MYSQL_RES * result );
  4. // Slow down the number of rows actually returned
  5. MYSQL_ROW mysql_fetch_row (MYSQL_RES * result );
  6. // Obtain the struct of the result from mysql_store_result () and retrieve a single row from it. If there is no more data or an error occurs, NULL is returned.
  7. Void mysql_free_result (MYSQL_RES * result );
  8. // Sort the allocated objects in the mySQL database and close the connection.
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; // the actual number of returned rows MYSQL_ROW mysql_fetch_row ); // obtain the struct of the result from mysql_store_result () and retrieve a single row from it. If there is no more data or an error occurs, return NULLvoid mysql_free_result (MYSQL_RES * result ); // sort the allocated objects in the mySQL database and close the connection.

Example:


[Cpp]View plaincopyprint?
  1. # Include <iostream>
  2. # Include <fstream>
  3. # Include <cstdlib>
  4. # Include <mysql/mysql. h>
  5. Using namespace std;
  6. Void mysql_err_function (MYSQL * connection );
  7. Int main ()
  8. {
  9. MYSQL * connection;
  10. Connection = mysql_init (NULL );
  11. If (! Connection)
  12. {
  13. Mysql_err_function (connection );
  14. }
  15. Connection = mysql_real_connect (connection, "localhost", "root", "123456", "test", 0, NULL, 0 );
  16. If (! Connection)
  17. {
  18. Mysql_err_function (connection );
  19. }
  20. Cout <"Connection to MySQL Server is Success..." <endl;
  21. String query;
  22. Getline (cin, query );
  23. Int res = mysql_query (connection, query. c_str ());
  24. If (res)
  25. {
  26. Mysql_err_function (connection );
  27. }
  28. MYSQL_RES * my_res = mysql_store_result (connection );
  29. Cout <"Retrieved" <mysql_num_rows (my_res) <"rows" <endl;
  30. MYSQL_ROW sqlrow;
  31. While (sqlrow = mysql_fetch_row (my_res )))
  32. {
  33. Cout <"Fetched data..." <endl;
  34. }
  35. Mysql_free_result (my_res );
  36. Mysql_close (connection );
  37. Cout <"Connection to MySQL Server is closed! "<Endl;
  38. Return 0;
  39. }
  40. Void mysql_err_function (MYSQL * connection)
  41. {
  42. If (mysql_errno (connection ))
  43. {
  44. Cout <"Error" <mysql_errno (connection) <":"
  45. <Mysql_error (connection) <endl;
  46. Exit (-1 );
  47. }
  48. }
#include <iostream>#include <fstream>#include <cstdlib>#include <mysql/mysql.h>using 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_res))){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);}}

2. extract a row of data at a time to process a large number of datasets. [Cpp]View plaincopyprint?
  1. MYSQL_RES * mysql_use_result (MYSQL * connection );
  2. // The result set is returned successfully. If the result set fails, NULL is returned.
MYSQL_RES * mysql_use_result (MYSQL * connection); // The result set is returned successfully. If the result set fails, NULL is returned.

Retrieving all data at a time increases network load and latency, but ensures data integrity.

Example:

[Cpp]View plaincopyprint?
  1. # Include <iostream>
  2. # Include <cstdlib>
  3. # Include <mysql/mysql. h>
  4. Using namespace std;
  5. Void mysql_err_function (MYSQL * connection );
  6. Int main ()
  7. {
  8. MYSQL * connection;
  9. Connection = mysql_init (NULL );
  10. If (mysql_real_connect (connection, "localhost", "root", "123456", "test", 0, NULL, 0 ))
  11. {
  12. Cout <"Connection to MySQL Server is Succeed..." <endl;
  13. String query;
  14. Getline (cin, query );
  15. Int res = mysql_query (connection, query. c_str ());
  16. If (res)
  17. {
  18. Mysql_err_function (connection); // For the implementation code of mysql_err_function (), refer to the previous example.
  19. }
  20. Else
  21. {
  22. MYSQL_RES * my_res = mysql_use_result (connection );
  23. If (my_res)
  24. {
  25. MYSQL_ROW sqlrow;
  26. While (sqlrow = mysql_fetch_row (my_res )))
  27. {
  28. Cout <"Fetching the Data..." <endl;
  29. }
  30. Mysql_free_result (my_res );
  31. }
  32. Else
  33. {
  34. Mysql_err_function (connection );
  35. }
  36. }
  37. Mysql_close (connection );
  38. Cout <"Connection to MySQL Server is Closed! "<Endl;
  39. }
  40. Else
  41. {
  42. Mysql_err_function (connection );
  43. }
  44. }

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.