C connection to the Mysql database code

  • 2020-04-02 01:54:35
  • OfStack


//Vc tool to add E:WAMPBINMYSQLMYSQL5.5.8LIB path & ;
 //Add libmysql.lib  in the project Settings - link library module; & have spent
#include <stdio.h>  
#include <time.h>  
#include <string.h>  
#include <winsock.h>  
#include "E:wampbinmysqlmysql5.5.8includemysql.h"  
void main(){   
       MYSQL *conn;   
       MYSQL_RES *res;   
       MYSQL_ROW row;   
       char *server ="localhost";   
       char *user ="root";   
       char *password="";   
       char *database="test";   
       char sql[1024]="select * from chinaren";   
       conn=mysql_init(NULL);   

       if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){   
               fprintf(stderr,"%sn",mysql_error(conn));   
               exit(1);   
       }   

       if(mysql_query(conn,sql)){   
               fprintf(stderr,"%sn",mysql_error(conn));   
               exit(1);   
       }   

       res=mysql_use_result(conn);   

       while((row = mysql_fetch_row(res))!=NULL){   
               printf("%sn",row[2]);   
       }   

       mysql_free_result(res);   
       mysql_close(conn);   
} 

Related articles: