int main(void) { //init the mysql parameter mysql_init(&mysql); //connect the database if(!mysql_real_connect(&mysql,"127.0.0.1","root","111","mytest",3306,NULL,0)) { printf(mysql_error(&mysql)); printf("\nCannot access to the database!!!\n"); system("pause"); exit(-1); }
//construct the query SQL statements char* sql="select * from student where name='"; char dest[100]={""}; strcat(dest,sql); printf("Please enter the student name:"); char name[10]={""}; gets(name); strcat(dest,name); strcat(dest,"'");
//excute the SQL statements if(mysql_query(&mysql,dest)) { printf("Cannot access the database with excuting \"%s\".",dest); system("pause"); exit(-1); }
//deal with the result result=mysql_store_result(&mysql); if(mysql_num_rows(result)) { while((row=mysql_fetch_row(result))) { printf("%s\t%s\t%s\n",row[0],row[1],row[2]); } } //release the resource mysql_free_result(result); mysql_close(&mysql);