#include<mysql/mysql.h> #include<stdio.h> #define BUFSIZE 4096 char *db_host="127.0.0.1"; unsigned int db_port=3306; char *db_user=NULL; char *db_passwd=NULL; char *database=NULL; char *db_table=NULL; char *db_field=NULL; char *db_match_field=NULL; char *db_match_content=NULL; char *file_name=NULL; char buffer[BUFSIZE]={0}; MYSQL *fp; MYSQL_RES *result; MYSQL_ROW row; static void usage(void) { fprintf(stderr,"Usage:\nFunction [-h host][-p port][-d db][-u user][-s passwd][-t ta ble][-n field][-m match_field][-i match_content][-f file_name]\n"); exit(0); } static void deal_with_arg(int argc,char **argv) { int c; while(1){ c=getopt(argc,argv,"h:p:d:u:s:t:n:m:i:f:?"); if(c==-1) break; switch(c){ case 'h':db_host=optarg;break; case 'p':db_port=atoi(optarg);break; case 'd':database=optarg;break; case 'u':db_user=optarg;break; case 's':db_passwd=optarg;break; case 't':db_table=optarg;break; case 'n':db_field=optarg;break; case 'm':db_match_field=optarg;break; case 'i':db_match_content=optarg;break; case 'f':file_name=optarg;break; case '?':usage(); default:usage(); } }
} int db_open(char host[],int port,char *user,char *passwd,char *db) { if((fp=mysql_init(NULL))==NULL){ fprintf(stderr,"[%s][%d]mysql_init failed!\n",__func__,__LINE__); return 1; } fprintf(stderr,"[%s][%d]mysql_init success!\n",__func__,__LINE__); if(mysql_real_connect(fp,host,user,passwd,db,port,NULL,0)==NULL){ fprintf(stderr,"[%s][%d]mysql_connect failed!\n",__func__,__LINE__); return 1; } fprintf(stderr,"[%s][%d]mysql_connect success!\n",__func__,__LINE__); return 0; } int db_query(MYSQL *fp,char *NeedField,char *TblName,char *MatchFieldName,char*MatchFieldVal ue) { char *query="select %s from %s where %s = \'%s\'"; char buf[BUFSIZE]={0}; char *db_state=NULL; sprintf(buf, query, NeedField, TblName, MatchFieldName, MatchFieldValue); db_state=mysql_stat(fp); if(db_state) fprintf(stderr,"[%s][%d][%s]mysql_state success\n",__func__,__LINE__,db_stat e);
if(mysql_real_query(fp, buf, strlen(buf)) != 0){ fprintf(stderr,"[%s][%d]mysql_query failed!\n",__func__,__LINE__); return 1; } // fprintf(stderr,"[%s][%d]mysql_query success\n",__func__,__LINE__); if((result=mysql_store_result(fp))==NULL){ fprintf(stderr,"[%s][%d]mysql_store_result failed!\n",__func__,__LINE__); return 1; } // fprintf(stderr,"[%s][%d]mysql_store_result success!\n",__func__,__LINE__); int field_num; if((field_num=mysql_num_fields(result))==0){ fprintf(stderr,"[%s][%d]mysql_field is zero!\n",__func__,__LINE__); return 1; } fprintf(stderr,"[%s]query_result:\n",__func__); while((row=mysql_fetch_row(result))!=NULL){ int i; for(i=0;i<field_num;++i){ printf("%s\t",row[i]); strncpy(buffer,row[i],strlen(row[i])); } printf("\n"); } return 0; } static int DBToFile(char *buf,int len,char *file) { FILE *fp; if(NULL==file){ fprintf(stderr,"[%s][%d]NO file!\n",__func__,__LINE__); return 1; } if((fp=fopen(file,"w+"))==NULL){ fprintf(stderr,"[%s][%d]fopen error!\n",__func__,__LINE__); return 1; } if(fwrite(buf,len,1,fp)==0){ fprintf(stderr,"[%s][%d]fwrite error!\n",__func__,__LINE__); return 1; } fclose(fp); return 0; } int main(int argc,char **argv) { deal_with_arg(argc,argv); // printf("[%s][%d][%s][%s][%s]\n",db_host,db_port,db_user,db_passwd,database); if(!(db_user&&db_passwd&&database&&db_table&&db_field&&db_match_field&&db_match_cont ent)){ usage(); }
if(db_port<0||db_port>65535){ fprintf(stderr,"[%s][%d]db_port fault!\n",__func__,__LINE__); exit(1); } if(db_open(db_host,db_port,db_user,db_passwd,database)){ fprintf(stderr,"[%s][%d]db_open error!\n",__func__,__LINE__); exit(1); } // fprintf(stderr,"[%s][%d]\n",__func__,__LINE__); if(db_query(fp,db_field,db_table,db_match_field,db_match_content)){ fprintf(stderr,"[%s][%d]db_query error!\n",__func__,__LINE__); exit(1); } // fprintf(stderr,"[%s][%d]\n",__func__,__LINE__); mysql_free_result(result); result=NULL; mysql_close(fp); if(DBToFile(buffer,strlen(buffer),file_name)){ fprintf(stderr,"[%s][%d]transDBToFile failed!\n",__func__,__LINE__); exit(1); } exit(0); }
|