MySQL学习笔记_12_Linux下C++/C连接MySQL数据库(二) --返回数据的SQL

来源:互联网 发布:淘宝争议处理规则 编辑:程序博客网 时间:2024/06/18 10:24


LinuxC++/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?在CODE上查看代码片派生到我的代码片
  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数据库整理分配的对象,关闭连接.  

示例:


[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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. }  

2、一次提取一行数据,用于处理了大量的数据集

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. MYSQL_RES *mysql_use_result(MYSQL * connection);    
  2. //成功返回结果集,失败返回NULL   

    一次取全部数据增加了网络负载,增加了时延,但是可以保证数据的完整性。

示例:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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. }  
0 0
原创粉丝点击