日期:2014-05-16 浏览次数:20637 次
int main(void) { MYSQL mysql; MYSQL_RES *result; MYSQL_ROW rows; /*init the connected handle*/ mysql_init(&mysql); /* Connect to database */ if (!mysql_real_connect(&mysql,"localhost","root","123456","test", 0, NULL, 0)) { fprintf(stderr,"Can not connect:%s\n"); exit(0); } else { printf("connection successful\n"); } /* send SQL query */ if((mysql_query(&mysql,"SELECT id,name from persons"))) { fprintf(stdout,"Error in query\n"); exit(0); } if(!(result=mysql_store_result(&mysql))) { fprintf(stderr,"Error reading result set\n"); } while((rows = mysql_fetch_row(result))) { fprintf(stdout,"%s - %s\n",rows[0],rows[1]); } mysql_free_result(result); mysql_close(&mysql); return 0; }