mysql c API訪問數據庫

 

#include <winsock2.h>

#include "CMySQL.h"

#include "stdio.h"

#define MYSQL_PORT 3306

void process_result_set(MYSQL mysql, MYSQL_RES *result);

void exit(MYSQL mydata,char *ep);

int main(int argc, char * argv[]) {

 MYSQL mydata;

 Database_Param p;

 char queryName[600];

 MYSQL_RES *result=NULL;

 int status =0;

 //初始化

 strcpy(p.host,"127.0.0.1");

 strcpy(p.user,"root");

 strcpy(p.password,"root");

 strcpy(p.db,"im");

 p.port = MYSQL_PORT;

 //初始化數據結構

 if(mysql_init(&mydata) == NULL)

 {

  printf("init mysql data stauct fail\n");

  system("pause");

  return -1;

 }

 //連接數據庫

 if(argc == 1)

 {

  if(NULL == mysql_real_connect(&mydata,p.host,p.user,p.password,

   p.db,p.port,NULL,CLIENT_MULTI_STATEMENTS))

  {

   printf("connect database fail,%s\n",mysql_error(&mydata));

   system("pause");

   return -1;

  }

 }

 else

 {

  printf("run parameter error\n");

  system("pause");

  return -1;

 }

 //首先設置字符集

 strcpy(queryName,"set names gbkd");

 if(mysql_query(&mydata,"set names gbk")!=0)

 {

  exit(mydata,"set names fail");

  return -1;

 }

 //查詢數據(支持單個字段和多個字段,輸出排版)

 strcpy(queryName,"show databases");

 if(mysql_query(&mydata,queryName) != 0)

 {

  exit(mydata,"execute sql syntax fail");

  return -1;

 }

 //取得查詢結果

 result= mysql_store_result(&mydata);

 process_result_set(mydata,result);

 //處理多條插入語句,在mysql_real_connect(……)函數裡的最後一個參數更改為:CLIENT_MULTI_STATEMENTS

 strcpy(queryName,"insert into fangl.admin(username,password) values('fangl1','fangl1'),('fangl2','fangl2'),('fangl3','fangl3');\

 insert into fangl.admin(username,password) values('fl1','fl1'),('fl2','fl2'),('fl3','fl3');\

 insert into fangl.admin(username,password) values('fal1','fal1'),('fal2','fal2'),('fal3','fal3');\

 select * from fangl.admin");

 if(mysql_query(&mydata,queryName)!=0)

 {

  exit(mydata,"insert values fail");

  return -1;

 }

 do {

  /* did current statement return data? */

  result = mysql_store_result(&mydata);

  if (result)

  {

   /* yes; process rows and free the result set */

   process_result_set(mydata, result);

   mysql_free_result(result);

  }

  else          /* no result set or error */

  {

   if (mysql_field_count(&mydata) == 0)

   {

    printf("%lld rows affected\n",

     mysql_affected_rows(&mydata));

   }

   else  /* some error occurred */

   {

    printf("Could not retrieve result set\n");

    break;

   }

  }

  /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */

  if ((status = mysql_next_result(&mydata)) > 0)

   printf("Could not execute statement\n");

 } while (status == 0);

 //刪除表中的數據

 strcpy(queryName,"delete from fangl.admin where username='fl1'");

 if(mysql_query(&mydata,queryName) != 0)

 {

  exit(mydata,"execute sql syntax fail");

  return -1;

 }

 //查詢數據(支持單個字段和多個字段,輸出排版)

 strcpy(queryName,"select * from fangl.admin");

 if(mysql_query(&mydata,queryName) != 0)

 {

  return -1;

 }

 //取得查詢結果

 result= mysql_store_result(&mydata);

 process_result_set(mydata,result);

 

 //釋放結果

 if(result != NULL)

 {

  mysql_free_result(result);

 }

 mysql_close(&mydata);

 system("pause");

 return 1;

}

void process_result_set(MYSQL mydata, MYSQL_RES *result)

{

 int rowcount = mysql_num_rows(result);       //查詢結果有多少行

 //取得各字段名

 MYSQL_FIELD *fields = NULL;

 for(int i=0;fields=mysql_fetch_field(result);i++)

 {

  printf("%s ",fields->name);

 }

 printf("\n");

 //依次讀取各條記錄

 MYSQL_ROW currow = NULL;

 while((currow = mysql_fetch_row(result)) != NULL)

 {

  for(int i = 0; i < mysql_num_fields(result); ++i)

  {

   printf("%s ",currow[i]?currow[i]:"NULL");

  }

  printf("\n");

 }

}

void exit(MYSQL mydata,char *ep)

{

 mysql_close(&mydata);

 printf("%s,%s\n",ep,mysql_error(&mydata));

 system("pause");

}

以上是在vs 2008下編譯並驗證無誤的,若有問題大傢一起討論,哈

本文出自 “lynn” 博客

You May Also Like