Chinaunix首页 | 论坛 | 博客
  • 博客访问: 227026
  • 博文数量: 39
  • 博客积分: 420
  • 博客等级: 下士
  • 技术积分: 457
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-01 10:22
文章分类

全部博文(39)

文章存档

2015年(15)

2014年(11)

2013年(1)

2012年(2)

2011年(1)

2010年(9)

我的朋友

分类: LINUX

2010-11-09 00:14:56

  1. /*  select1.c */  
  2.   
  3. #include    
  4. #include    
  5. #include    
  6. #include "/usr/include/mysql/mysql.h"  
  7.   
  8. int main(int argc, char *argv[])   
  9. {   
  10. MYSQL my_connection;   
  11. MYSQL_RES *res_ptr;   /*指向检索的结果存放地址的指针*/  
  12. MYSQL_ROW sqlrow;     /*返回的记录信息*/  
  13. MYSQL_FIELD *fd;      /*字段结构指针*/  
  14. char aszflds[25][25]; /*用来存放各字段名*/  
  15. int res;             /*执行查询操作后的返回标志*/  
  16. int i,j,k;   
  17.   
  18. mysql_init(&my_connection);   
  19.   
  20. /*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/  
  21. if (mysql_real_connect(&my_connection, "localhost""root""yourpasswd","cusemysql",0,NULL,CLIENT_FOUND_ROWS))   
  22. {   
  23.     printf("Connection success\n");   
  24.     res = mysql_query(&my_connection, "select childno,fname,age from children where age<30");   
  25.   
  26.     if (res)   
  27.     {   
  28.         printf("SELECT error:%s\n",mysql_error(&my_connection));   
  29.     }   
  30.     else  
  31.     {   
  32.        res_ptr=mysql_store_result(&my_connection);   
  33.        if(res_ptr)   
  34.        {   
  35.               printf("Retrieved %lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));   
  36.               /*取得各字段名*/  
  37.               for(i=0;fd=mysql_fetch_field(res_ptr);i++)   
  38.                      strcpy(aszflds[i],fd->name);   
  39.               /*输出各条记录*/  
  40.               printf("下面是检索出的各条记录信息:\n");   
  41.               j=mysql_num_fields(res_ptr);   
  42.               for(i=0;i
  43.               printf("%s\t",aszflds[i]);   
  44.               printf("\n");   
  45.               while((sqlrow=mysql_fetch_row(res_ptr)))   
  46.               {   
  47.                      for(i=0;i
  48.                      printf("%s\t",sqlrow[i]);   
  49.                      printf("\n");   
  50.               }   
  51.               if (mysql_errno(&my_connection))   
  52.               {   
  53.                      fprintf(stderr,"Retrive error:s\n",mysql_error(&my_connection));   
  54.               }   
  55.        }   
  56.        mysql_free_result(res_ptr);   
  57.        }   
  58.     mysql_close(&my_connection);   
  59. }   
  60.   
  61. else  
  62. {   
  63.     fprintf(stderr, "Connection failed\n");   
  64.   
  65.     if (mysql_errno(&my_connection))   
  66.     {   
  67.         fprintf(stderr, "Connection error %d: %s\n",   
  68.         mysql_errno(&my_connection),   
  69.         mysql_error(&my_connection));   
  70.         }   
  71. }   
  72.     return EXIT_SUCCESS;   
  73. }   

编译方法之一:gcc  -o select1 select1.c `mysql_config --cflags --libs`
下面是我机子上的演示:
root@pipal-desktop:/home/pipal/mysql# gcc select1.c -o select1 `mysql_config --cflags --libs`
root@pipal-desktop:/home/pipal/mysql# ./select1
Connection success
Retrieved 3 Rows
下面是检索出的各条记录信息:
childno fname   age
5       flower  10
10      Ann     5
20      花儿    22
此文章来自:http://pipal.javaeye.com/blog/288132

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