MySQL學習筆記_12_Linux下C++/C串連MySQL資料庫(二) --返回資料的SQL

來源:互聯網
上載者:User

標籤:c api   c++   sql   mysql   connection   

Linux下C++/C串連MySQL資料庫(二)--返回資料的SQL引:

    返回資料的SQL是指通過查詢語句從資料庫中取出滿足條件的資料記錄

從MySQL資料庫值哦功能檢索資料有4個步驟:

    1)發出查詢

    2)檢索資料

    3)處理資料

    4)整理所需要的資料

用mysql_query()發出查詢,檢索資料可以使用mysql_store_result()或mysql_use_result(),取決與怎樣檢索資料,接著是調用mysql_fetch_row()來處理資料,最後,還必須調用mysql_free_result()以允許MySQL進行必要的整理工作。


1、一次提取所有資料    [cpp] view plaincopyprint?
  1. MYSQL_RES *mysql_store_result(MYSQL * connection);  
  2. //成功返回結構體指標,失敗返回NULL  
  3.     my_ulonglong mysql_num_row(MYSQL_RES * result);  
  4. //減速實際返回的行數  
  5.     MYSQL_ROW mysql_fetch_row(MYSQL_RES * result);  
  6. //從mysql_store_result()中得到結果的結構體,並從中檢索單個行,當沒有更多的資料,或者出錯時,返回NULL  
  7.     void mysql_free_result(MYSQL_RES * result);  
  8. //使mySQL資料庫清理分配的對象,關閉串連.  
MYSQL_RES *mysql_store_result(MYSQL * connection);//成功返回結構體指標,失敗返回NULLmy_ulonglong mysql_num_row(MYSQL_RES * result);//減速實際返回的行數MYSQL_ROW mysql_fetch_row(MYSQL_RES * result);//從mysql_store_result()中得到結果的結構體,並從中檢索單個行,當沒有更多的資料,或者出錯時,返回NULLvoid mysql_free_result(MYSQL_RES * result);//使mySQL資料庫清理分配的對象,關閉串連.

樣本:


[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. #include <fstream>  
  3. #include <cstdlib>  
  4. #include <mysql/mysql.h>  
  5. using namespace std;  
  6.   
  7. void mysql_err_function(MYSQL * connection);  
  8.   
  9. int main()  
  10. {  
  11.     MYSQL * connection;  
  12.     connection = mysql_init(NULL);  
  13.   
  14.     if (!connection)  
  15.     {  
  16.         mysql_err_function(connection);  
  17.     }  
  18.   
  19.     connection = mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0);  
  20.   
  21.     if (!connection)  
  22.     {  
  23.         mysql_err_function(connection);  
  24.     }  
  25.   
  26.     cout << "Connection to MySQL Server is Success..." << endl;  
  27.     string query;  
  28.     getline(cin,query);  
  29.   
  30.     int res = mysql_query(connection,query.c_str());  
  31.     if (res)  
  32.     {  
  33.         mysql_err_function(connection);  
  34.     }  
  35.   
  36.     MYSQL_RES * my_res = mysql_store_result(connection);  
  37.   
  38.     cout << "Retrieved " << mysql_num_rows(my_res) << "rows" << endl;  
  39.   
  40.     MYSQL_ROW sqlrow;  
  41.     while ((sqlrow = mysql_fetch_row(my_res)))  
  42.     {  
  43.         cout << "Fetched data..." << endl;  
  44.     }  
  45.     mysql_free_result(my_res);  
  46.   
  47.     mysql_close(connection);  
  48.     cout << "Connection to MySQL Server is closed!" << endl;  
  49.   
  50.     return 0;  
  51. }  
  52.   
  53. void mysql_err_function(MYSQL * connection)  
  54. {  
  55.     if (mysql_errno(connection))  
  56.     {  
  57.         cout << "Error " << mysql_errno(connection) << " : "  
  58.         << mysql_error(connection) << endl;  
  59.   
  60.         exit(-1);  
  61.     }  
  62. }  
#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、一次提取一行資料,用於處理了大量的資料集 [cpp] view plaincopyprint?
  1. MYSQL_RES *mysql_use_result(MYSQL * connection);    
  2. //成功返回結果集,失敗返回NULL   
MYSQL_RES *mysql_use_result(MYSQL * connection);  //成功返回結果集,失敗返回NULL 

    一次取全部資料增加了網路負載,增加了時延,但是可以保證資料的完整性。

樣本:

[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <mysql/mysql.h>  
  4. using namespace std;  
  5.   
  6. void mysql_err_function(MYSQL * connection);  
  7.   
  8. int main()  
  9. {  
  10.     MYSQL * connection;  
  11.     connection = mysql_init(NULL);  
  12.   
  13.     if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))  
  14.     {  
  15.         cout << "Connection to MySQL Server is Succeed..." << endl;  
  16.         string query;  
  17.         getline(cin,query);  
  18.   
  19.         int res = mysql_query(connection,query.c_str());  
  20.         if (res)  
  21.         {  
  22.             mysql_err_function(connection);//mysql_err_function()實現代碼參考上例  
  23.         }  
  24.         else  
  25.         {  
  26.             MYSQL_RES * my_res = mysql_use_result(connection);  
  27.             if (my_res)  
  28.             {  
  29.                 MYSQL_ROW sqlrow;  
  30.                 while ((sqlrow = mysql_fetch_row(my_res)))  
  31.                 {  
  32.                     cout << "Fetching the Data..." << endl;  
  33.                 }  
  34.   
  35.                 mysql_free_result(my_res);  
  36.             }  
  37.             else  
  38.             {  
  39.                 mysql_err_function(connection);  
  40.             }  
  41.         }  
  42.   
  43.         mysql_close(connection);  
  44.         cout << "Connection to MySQL Server is Closed!" << endl;  
  45.     }  
  46.     else  
  47.     {  
  48.         mysql_err_function(connection);  
  49.     }  
  50. }  

MySQL學習筆記_12_Linux下C++/C串連MySQL資料庫(二) --返回資料的SQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.