MySQL Learning note _12_linux under C++/C connection MySQL database (ii)--SQL to return data

Source: Internet
Author: User

 Linuxunderc++/cConnectionMySQLDatabase(two)--returns the dataSQLCited:

returns the data SQL a data record that satisfies a condition is fetched from the database by a query statement

from MySQL Database Value Oh function Retrieve data Yes 4 a step:

1 ) issue a query

2 ) Retrieve data

3 ) processing Data

4 ) to collate the required data

with mysql_query () issue a query that retrieves data that can be used Mysql_store_result () or Mysql_use_result () , depending on how the data is retrieved, and then the call mysql_fetch_row () to process the data, and finally, you must call the mysql_free_result () to allow MySQL perform the necessary finishing work.


1, extract all data at once [CPP]View Plaincopyprint?
  1. Mysql_res *mysql_store_result (MySQL * connection);
  2. //successfully returned struct pointer, failed to return null   
  3. My_ulonglong Mysql_num_row (Mysql_res * result);
  4. //Decelerate The actual number of rows returned   
  5. Mysql_row mysql_fetch_row (Mysql_res * result);
  6. //The struct that gets the result from Mysql_store_result () and retrieves a single row from it, returning NULL when there is no more data, or when there is an error   
  7. void  mysql_free_result (Mysql_res * result);
  8. //Make the MySQL database tidy up the allocated objects and close the connection.   
Mysql_res *mysql_store_result (MySQL * connection);//successful return of struct pointer, failed to return Nullmy_ulonglong mysql_num_row (mysql_res * result) ;//decelerate the number of rows actually returned Mysql_row mysql_fetch_row (mysql_res * result),//The structure from Mysql_store_result (), and retrieve a single row from it, when no more data is available, or error, return nullvoid mysql_free_result (mysql_res * result);//make the MySQL database tidy up the allocated objects 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_que    Ry (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) & lt;< ":" << mysql_error (Connection) << Endl;exit (-1);}}

2, fetching one row of data at a time for processing a large number of data sets [CPP]View Plaincopyprint?
    1. Mysql_res *mysql_use_result (MySQL * connection);
    2. //Successfully returned result set, failed to return null   
Mysql_res *mysql_use_result (MySQL * connection);  

Taking all the data at once increases the network load and increases the latency, but it can guarantee the integrity of the data.

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); //mysql_err_function () Implementation code reference above 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. }

MySQL Learning note _12_linux under C++/C connection MySQL database (ii)--SQL to return data

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.