查询mysql数据库
#include "stdio.h" #include "winsock.h" #include "mysql.h" int main(){ MYSQL * con; MYSQL_RES *res; MYSQL_ROW row; char dbuser[]="root"; char dbpasswd[]="123456"; char dbip[]="localhost"; char dbname[]="test"; con = mysql_init((MYSQL*) 0); if ( con !=NULL && mysql_real_connect(con,dbip,dbuser,dbpasswd,dbname,3306/*TCP IP端口*/,NULL/*Unix Socket 连接类型*/,0/*运行成ODBC数据库标志*/) ){ if (!mysql_select_db(con,dbname)){ con ->reconnect = 1; char *query="set names \'gbk\'"; mysql_real_query(con,query,strlen(query)); } } char tmp[]="select ename from emp"; mysql_real_query(con,tmp,strlen(tmp)); res = mysql_store_result(con);//将结果保存在res结构体中 int count=0,t=0; while(row = mysql_fetch_row(res)){ for(t=0; t<mysql_num_fields(res); t++){ printf("%s\n",row[t]); } } mysql_free_result(res); mysql_close(con); return 0; }