1. 程式人生 > >這裡是MYSQL操作的C語言程式碼,我貼幾個,你們看吧

這裡是MYSQL操作的C語言程式碼,我貼幾個,你們看吧

#include <stdio.h>
#include <string.h>

#include <windows.h>
#include <mysql.h>

int main(void )
{
  int id;
  MYSQL mysql;
  MYSQL_ROW row;
  MYSQL_RES * res;
  char sqlbuf[256];

  /*init mysql*/

 if(mysql_init(&mysql)== NULL){
   fprintf(stderr,"%s %d: mysql init failed!/n", __FILE__,__LINE__);
   return -1;
  }

 /* connect mysql*/

 if(mysql_real_connect(&mysql,"localhost","root","","example",3306,NULL,0)== NULL){
   fprintf(stderr,"%s%d:mysql connect failed !/n",__FILE__,__LINE__);
   return -1;
 }

/* query mysql*/

 sprintf(sqlbuf,"select * from %s ","viewmsg");
 if(mysql_real_query(&mysql,sqlbuf,strlen(sqlbuf))!= 0){
  fprintf(stderr,"%s%d:mysq; query failed!/n",__FILE__,__LINE__);
   return -1;
 }

/*store mysql result */

if((res=mysql_store_result(&mysql))==NULL){
  fprintf(stderr,"%s %d : result NULL!/n",__FILE__,__LINE__);
  return -1;
  }

 while(row = mysql_fetch_row(res)){

     id = atoi(row[0]);
     fprintf(stderr,"Load web %d...[%s]/n",id,row[1]);
 }


 /* Free mysql result*/
 mysql_free_result(res);

/*close mysql*/

mysql_close(&mysql);

return 0;

}