Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49400
  • 博文数量: 25
  • 博客积分: 960
  • 博客等级: 准尉
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 12:39
文章分类
文章存档

2011年(1)

2009年(20)

2008年(4)

我的朋友

分类: Mysql/postgreSQL

2009-04-24 14:06:37

/*  select1.c */
#include
#include
#include
//#include "/usr/local/mysql/include/mysql/mysql.h"
#include "/usr/local/include/mysql/mysql.h"
int main(int argc, char *argv[])
{
MYSQL my_connection;
MYSQL_RES *res_ptr;   /*指向检索的结果存放地址的指针*/
MYSQL_ROW sqlrow;     /*返回的记录信息*/
MYSQL_FIELD *fd;      /*字段结构指针*/
char aszflds[25][25]; /*用来存放各字段名*/
int res;             /*执行查询操作后的返回标志*/
int i,j,k;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","test",0,NULL,CLIENT_FOUND_ROWS))
{
    printf("Connection success\n");
    res = mysql_query(&my_connection, "select * from student");
    if (res)
    {
        printf("SELECT error:%s\n",mysql_error(&my_connection));
    }
    else
    {
       res_ptr=mysql_store_result(&my_connection);
       if(res_ptr)
       {
              printf("Retrieved %lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));
              /*取得各字段名*/
              for(i=0;fd=mysql_fetch_field(res_ptr);i++)
                     strcpy(aszflds[i],fd->name);
              /*输出各条记录*/
              printf("下面是检索出的各条记录信息:\n");
              j=mysql_num_fields(res_ptr);
              for(i=0;i              printf("%s\t",aszflds[i]);
              printf("\n");
              while((sqlrow=mysql_fetch_row(res_ptr)))
              {
                     for(i=0;i                     printf("%s\t",sqlrow[i]);
                     printf("\n");
              }
              if (mysql_errno(&my_connection))
              {
                     fprintf(stderr,"Retrive error:s\n",mysql_error(&my_connection));
              }
       }
       mysql_free_result(res_ptr);
       }
    mysql_close(&my_connection);
}
else
{
    fprintf(stderr, "Connection failed\n");
    if (mysql_errno(&my_connection))
    {
        fprintf(stderr, "Connection error %d: %s\n",
        mysql_errno(&my_connection),
        mysql_error(&my_connection));
        }
}
    return EXIT_SUCCESS;
}
阅读(512) | 评论(0) | 转发(0) |
0

上一篇:C语言插入MySQL insert

下一篇:esql create table

给主人留下些什么吧!~~