MySQL学习笔记_13_Linux下C++/C连接MySQL数据库(三) --处理返回数据



LinuxC++/C连接MySQL数据库()

--处理返回数据


一、通过返回结果集中的字段数

  1. unsigned int mysql_field_count(MYSQL * connection);  
  2. //将MYSQL_ROW的值作为一个存储了一行数据的数组...  

示例:

  1. //一次取一个值的情况,另一种情况与其类似,修改处会标出  
  2. #include <iostream>  
  3. #include <fstream>  
  4. #include <cstdlib>  
  5. #include <mysql/mysql.h>  
  6. using namespace std;  
  7.   
  8. void mysql_err_function(MYSQL * connection);  
  9. void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow);  
  10.   
  11. int main()  
  12. {  
  13.     MYSQL * connection;  
  14.     connection = mysql_init(NULL);  
  15.   
  16.     if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))  
  17.     {  
  18.         cout << "Connection to MySQL Server is Succeed..." << endl;  
  19.         string query = "select * from tmp15";  
  20.         //getline(cin,query);  
  21.   
  22.         int res = mysql_query(connection,query.c_str());  
  23.         if (res)  
  24.         {  
  25.             mysql_err_function(connection);  
  26.         }  
  27.         else  
  28.         {  
  29.             MYSQL_RES * my_res = mysql_use_result(connection);    
  30. //将mysql_use_result改为mysql_store_result即可得到另一种情况的结果(其实是相同的...)  
  31.             if (my_res)  
  32.             {  
  33.                 MYSQL_ROW sqlrow;  
  34.                 while ((sqlrow = mysql_fetch_row(my_res)))  
  35.                 {  
  36.                     mysql_display(connection,sqlrow);  
  37.                 }  
  38.   
  39.                 mysql_free_result(my_res);  
  40.             }  
  41.             else  
  42.             {  
  43.                 mysql_err_function(connection);  
  44.             }  
  45.         }  
  46.   
  47.         mysql_close(connection);  
  48.         cout << "Connection to MySQL Server is Closed!" << endl;  
  49.     }  
  50.     else  
  51.     {  
  52.         mysql_err_function(connection);  
  53.     }  
  54. }  
  55.   
  56. void mysql_err_function(MYSQL * connection)  
  57. {  
  58.     if (mysql_errno(connection))  
  59.     {  
  60.         cout << "Error " << mysql_errno(connection) << " : "  
  61.              << mysql_error(connection) << endl;  
  62.   
  63.         exit(-1);  
  64.     }  
  65. }  
  66.   
  67. void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow)  
  68. {  
  69.     for (unsigned int i = 0; i < mysql_field_count(mysql); ++i)  
  70.     {  
  71.         printf("%s ",sqlrow[i]);  
  72.         //cout << sqlrow[i] << ‘ ‘; //不知到为什么将printf换成cout之后,打印值就会出错...思考ing...  
  73.     }  
  74.     cout << endl;  
  75. }  

二、获取一个字段的信息

  1. 1、MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result);    
  2.       
  3. 2、MYSQL_FIELD定义:  
  4. typedef struct st_mysql_field  
  5. {  
  6.     char *name;                          
  7. /* Name of column */  
  8.     char *table;                          
  9. /* Table of column if column was a field */  
  10.     char *org_table;                  
  11. /* Org table name if table was an alias */  
  12.     char *db;                          
  13. /* Database for table */  
  14.     char *def;                          
  15. /* Default value (set by mysql_list_fields) */  
  16.     unsigned long length;                  
  17. /* Width of column */  
  18.     unsigned long max_length;          
  19. /* Max width of selected set */  
  20.     unsigned int flags;                  
  21. /* Div flags */  
  22.     unsigned int decimals;          
  23. /* Number of decimals in field */  
  24.     enum enum_field_types type;          
  25. /* Type of field. Se mysql_com.h for types */  
  26. } MYSQL_FIELD;  
  27.       
  28. 3、IS_NUM宏,若字段类型是数字形式的,则返回真。  
  29. if (IS_NUM(mysql_field_ptr -> type))  
  30. {  
  31.     cout << "Number" << endl;  
  32. }  
  33.   
  34. 4、MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL_RES * result,  
  35. MYSQL_FIELD_OFFSET offset);  
  36. //函数将字段光标设置到给定的偏移量offset,下一次调用mysql_fetch_field将检索与该偏移量关联的列的字段定义。如果钥定位行的开始,则要传递一个值为0的offset值。  

示例:

  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. void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow); //实现参照上例  
  9. void mysql_display_head(MYSQL_RES * my_res);  
  10.   
  11. int main()  
  12. {  
  13.     MYSQL * connection;  
  14.     connection = mysql_init(NULL);  
  15.   
  16.     if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))  
  17.     {  
  18.         cout << "Connection to MySQL Server is Succeed..." << endl;  
  19.         string query = "select * from tmp15";  
  20.         //getline(cin,query);  
  21.   
  22.   
  23.         int res = mysql_query(connection,query.c_str());  
  24.         if (res)  
  25.         {  
  26.             mysql_err_function(connection);  
  27.         }  
  28.         else  
  29.         {  
  30.             MYSQL_RES * my_res = mysql_use_result(connection);  
  31.             if (my_res)  
  32.             {  
  33.                 mysql_display_head(my_res);  
  34.   
  35.                 MYSQL_ROW sqlrow;  
  36.                 cout << "Column Details:" << endl;  
  37.                 while ((sqlrow = mysql_fetch_row(my_res)))  
  38.                 {  
  39.                     mysql_display(connection,sqlrow);  
  40.                 }  
  41.   
  42.                 mysql_free_result(my_res);  
  43.             }  
  44.             else  
  45.             {  
  46.                 mysql_err_function(connection);  
  47.             }  
  48.         }  
  49.   
  50.         mysql_close(connection);  
  51.         cout << "Connection to MySQL Server is Closed!" << endl;  
  52.     }  
  53.     else  
  54.     {  
  55.         mysql_err_function(connection);  
  56.     }  
  57. }  
  58.   
  59. void mysql_display_head(MYSQL_RES * my_res)  
  60. {  
  61.     MYSQL_FIELD * my_field;  
  62.     cout << "Column Describe:" << endl;  
  63.     while ((my_field = mysql_fetch_field(my_res)))  
  64.     {  
  65.         cout << "\tName: " << my_field -> name << endl;  
  66.         cout << "\tType: ";  
  67.         if (IS_NUM(my_field -> type))  
  68.         {  
  69.             cout << "Numeric field";  
  70.         }  
  71.         else  
  72.         {  
  73.             switch(my_field -> type)  
  74.             {  
  75.             case FIELD_TYPE_VAR_STRING:  
  76.                 cout << "Varchar";  
  77.                 break;  
  78.             case FIELD_TYPE_LONG:  
  79.                 cout << "Long";  
  80.                 break;  
  81.             default:  
  82.                 cout << "is " << my_field -> type << ",check in mysql.h";  
  83.                 break;  
  84.             }  
  85.         }  
  86.         cout << endl;  
  87.   
  88.         cout << "\tMax width: " << my_field -> length << endl;  
  89.         if (my_field -> flags & AUTO_INCREMENT_FLAG)  
  90.         {  
  91.             cout << "\tAUTO_INCREMENT" << endl;  
  92.         }  
  93.         cout << endl;  
  94.     }  
  95. }  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。