Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4710437
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: Mysql/postgreSQL

2010-05-03 11:04:42

 

#include <stdio.h>
#include <unistd.h>
#include <mysql.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1000000
#define bool int
#define true 1
#define false 0

bool Connect_Server(MYSQL **sql,char *host,char *user,char *passwd,char *dbname)
{
        my_bool b = 0;
        if(!(*sql=mysql_init(NULL)))
        {
                printf("%s\n",mysql_error(*sql));
                return 0;
        }
        mysql_options(*sql,MYSQL_REPORT_DATA_TRUNCATION,&b);
        if(!mysql_real_connect(*sql,host,user,passwd,dbname,0,NULL,0))
        {
                fprintf(stderr,"%s\n",mysql_error(*sql));
                return 0;
        }
        return 1;// true;

}

bool Execute(MYSQL *mysql,MYSQL_RES **res,char *query,int length)
{
if(length!=0)
{
   if(mysql_real_query(mysql,query,length))
   {
    printf("query error!\n");
    return false;
   }
}
else
{
   if(mysql_query(mysql,query))
   {
    printf("%s","query error!\n");
    return false;
   }
}
*res = mysql_store_result(mysql);
if(!*res)
{
   if(mysql_field_count(mysql)>0)
   {
    printf("%s\n", "store_result error!");
    return false ;
   }

}
return true;
}

bool Select_Row(MYSQL_ROW *row,MYSQL_RES *res,my_ulonglong num_row)
{
if(num_row>mysql_num_rows(res)||num_row<0)
{
   printf("%s\n", "Select row error!");
   return false;
}
/*void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);
 offset
*/

//mysql_data_seek(res,num_row);

*row=mysql_fetch_row(res);
if(*row==NULL)
{
   printf("%s\n", "fetch_row error!");
   return false;
}
return true;
}

bool Next_Row(MYSQL_ROW *row,MYSQL_RES *res)
{
*row=mysql_fetch_row(res);
if(*row==NULL)
{
  // printf("%s\n", "fetch_row error!");

   return false;
}
return true;
}

unsigned long Num_Rows(MYSQL_RES *res)
{
return (unsigned long)mysql_num_rows(res);
}

void Free_Result(MYSQL_RES *res)
{
mysql_free_result(res);
}

void Mysql_Close(MYSQL *sql)
{
        mysql_close(sql);
}

int main(void)
{
char query[200];
//STEP 1:define varible

MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
/////////////////////////////////////////////////////////////////////////////////////


//STEP 2:connect to database

//"127.0.0.1", server ip;

//"root" and "123456",usename and passwd

//"test",which database you want to use

if(!Connect_Server(&mysql,"127.0.0.1", "root", "123456", "test"))
{
   return 0;
}

//////////////////////////////////////////////////////////////////////////////////////

        
//STEP 3:query

////////query cmd.

strcpy(query,"SELECT id FROM info");
/////////execute query

if(!Execute(mysql,&res,query,0))
{
   return 0;
}
///////////////////////////////////////////////////////////////////////////////////////


//STEP 4:fetch query result row by row

printf("%lu rows in result.\n",Num_Rows(res));

printf("\nGo on search in result...\n");

while(Next_Row(&row,res))
{
   printf("ser_no=%s\n",row[0]);
}

 Free_Result(res);
////////////////////////////////////////////////////////////////////////////////////////


//STEP 5:close connect

Mysql_Close(mysql);

 return 0;
}

结果
[kenthy@kenthy mysql]$ gcc -Wall -o mysql1 mysql1.c -I/usr/include/mysql/ -L/usr/lib/mysql/ -lmysqlclient -lz
[kenthy@kenthy mysql]$ ./mysql1
10 rows in result.

Go on search in result...
ser_no=1
ser_no=2
ser_no=3
ser_no=4
ser_no=5
ser_no=6
ser_no=7
ser_no=8
ser_no=9
ser_no=10


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

chinaunix网友2010-06-01 06:23:19

MBT is based on Africa's indigenous people to create innovative walking barefoot, and wear this shoe can stimulate the body to relax the muscles so that the muscles of heat to be released, consumed calories, thus to achieve weight loss results. MBT's pursuit of beauty in order to satisfy customers, design a variety of styles, such as Mbt Chapa shoe, MBT Lami Shoe