文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>自己写的mysql基本api

自己写的mysql基本api

时间:2010-05-03  来源:ubuntuer

 

#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


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载