Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103088
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 188
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-23 19:19
文章分类

全部博文(33)

文章存档

2015年(22)

2014年(11)

我的朋友

分类: C/C++

2014-12-30 12:26:38

(小豪之家大神的)(http://blog.csdn.net/small_qch/article/details/8180678)
[cpp]
 view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //test.c  
  2. //gcc test.c -o test -lmysqlclient  
  3. #include   
  4. #include   
  5. #include   
  6.   
  7. //发生错误时,输出错误信息,关闭连接,退出程序  
  8. void error_quit(const char *str, MYSQL *connection)  
  9. {  
  10.     fprintf(stderr, "%s : %d: %s\n",  
  11.         str, mysql_errno(connection),  
  12.         mysql_error(connection));  
  13.     if( connection != NULL )  
  14.         mysql_close(connection);  
  15.     exit(1);  
  16. }  
  17.   
  18. int main(int argc, char *argv[])   
  19. {  
  20.     MYSQL *my_con = malloc( sizeof(MYSQL) );  
  21.     MYSQL_RES *my_res;  
  22.     MYSQL_FIELD *my_field;  
  23.     MYSQL_ROW my_row;  
  24.     int rows, i;  
  25.     int res;  
  26.   
  27.     //连接数据库  
  28.     mysql_init(my_con);   
  29.     my_con = mysql_real_connect(my_con, "localhost""test""aaaaaaa",  
  30.         "test1", 0, NULL, CLIENT_FOUND_ROWS);  
  31.     if( NULL == my_con )   
  32.         error_quit("Connection fail", my_con);  
  33.     printf("Connection success\n");  
  34.   
  35.     //向数据库中插入一条记录  
  36.     res = mysql_query(my_con,   
  37.         "insert into class1(name, age, birthday) value('abc', 52, NOW());");  
  38.     if( res != 0 )   
  39.         error_quit("Insert fail", my_con);  
  40.     //返回的是表中被影响的行数  
  41.     res = mysql_affected_rows(my_con);  
  42.     printf("Inserted %d rows\n", res);  
  43.   
  44.     //获取整个表的内容  
  45.     res = mysql_query(my_con, "select * from class1;");  
  46.     if( res != 0 )  
  47.         error_quit("Select fail", my_con);  
  48.     my_res = mysql_store_result(my_con);  
  49.     if( NULL == my_res )  
  50.         error_quit("Get result fail", my_con);  
  51.   
  52.     //获取表的列数  
  53.     rows = mysql_num_fields(my_res);  
  54.     //获取并输出表头  
  55.     my_field = mysql_fetch_fields(my_res);  
  56.     for(i=0; i
  57.         printf("%s\t", my_field[i].name);  
  58.     printf("\n-------------------------------------\n");  
  59.   
  60.     //输出整个表的内容  
  61.     while( 1 )  
  62.     {  
  63.         my_row = mysql_fetch_row(my_res);  
  64.         if( NULL == my_row )  
  65.             break;  
  66.         for(i=0; i
  67.         {  
  68.             if( my_row[i] == NULL )  
  69.                 printf("NULL\t");  
  70.             else  
  71.                 printf("%s\t", (char*)my_row[i]);  
  72.         }  
  73.         printf("\n");  
  74.     }  
  75.   
  76.     //释放空间,关闭连接  
  77.     mysql_free_result(my_res);  
  78.     mysql_close(my_con);  
  79.     free(my_con);  
  80.   
  81.     return 0;  
  82. }  

  83. 运行结果:



    1. Connection success  
    2. Inserted 1 rows  
    3. id  name    age birthday  
    4. -------------------------------------  
    5. 49  ddd 43  2012-11-09 09:49:41  
    6. 50  fff 31  0000-00-00 00:00:00  
    7. 58  eee 32  NULL  
    8. 59  qqq 43  NULL  
    9. 78  abc 52  2012-11-13 14:47:55  

阅读(1171) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~