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?
- Mysql_res *mysql_store_result (MySQL * connection);
- //successfully returned struct pointer, failed to return null
- My_ulonglong Mysql_num_row (Mysql_res * result);
- //Decelerate The actual number of rows returned
- Mysql_row mysql_fetch_row (Mysql_res * result);
- //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
- void mysql_free_result (Mysql_res * result);
- //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?
- #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);
- }
- }
#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?
- Mysql_res *mysql_use_result (MySQL * connection);
- //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?
- #include <iostream>
- #include <cstdlib>
- #include <mysql/mysql.h>
- 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); //mysql_err_function () Implementation code reference above 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);
- }
- }
MySQL Learning note _12_linux under C++/C connection MySQL database (ii)--SQL to return data