?? mysqlnew.c
字號(hào):
#include <mysql.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#define CONFIG "submitdb.conf"int main(){ MYSQL mysql; char *query; int t = 0,q = 0; int flag=0; int count=0; FILE *fd,*configfp; char tmpbuf[255],configbuf[255]; char *ptr,*p,*w; char time[20],s[255],f[60],u[4096],tmp[4096]; char IP[64],Username[64],Password[64],db[64],table1[64],table2[64]; if((configfp=fopen(CONFIG,"r"))==NULL) { printf("configfile can not open!\n"); return -1; } while(!feof(configfp)){ if((fgets(configbuf, sizeof(configbuf), configfp)) == NULL){ break; } if((ptr=strstr(configbuf,"ServerIP:"))!=NULL) { ptr += strlen("ServerIP:"); w=ptr; while(*ptr != '#') ptr++; *ptr='\0'; strcpy(IP,w); } if((ptr=strstr(configbuf,"Username:"))!=NULL){ ptr+=strlen("Username:"); w=ptr; while(*ptr!='#') ptr++; *ptr='\0'; strcpy(Username,w); } if((ptr=strstr(configbuf,"Password:"))!=NULL){ ptr+=strlen("Password:"); w=ptr; while(*ptr!='#') ptr++; *ptr='\0'; strcpy(Password,w); printf("%s,",Password); } if((ptr=strstr(configbuf,"databasename:"))!=NULL){ ptr+=strlen("databasename:"); w=ptr; while(*ptr!='#') ptr++; *ptr='\0'; strcpy(db,w); printf("%s,",db); } if((ptr=strstr(configbuf,"table1:"))!=NULL){ ptr+=strlen("table1:"); w=ptr; while(*ptr!='#') ptr++; *ptr='\0'; strcpy(table1,w); } if((ptr=strstr(configbuf,"table2:"))!=NULL){ ptr+=strlen("table2:"); w=ptr; while(*ptr!='#') ptr++; *ptr='\0'; strcpy(table2,w); } } mysql_init(&mysql); if(!mysql_real_connect(&mysql,IP,Username,Password,db,3306,NULL,0)) { printf("Error connecting to database:%s\n",mysql_error(&mysql)); } else printf("Connected...\n"); if((fd = fopen("/sqlinformation", "r")) == NULL){ printf("ERROR: open file fail!!!\n"); return -1; } while(!feof(fd)){ if((fgets(tmpbuf, sizeof(tmpbuf), fd)) == NULL){ break; } count++; p=tmpbuf; while(*p!='\0') { while(*p!='\n') p++; *p='\0'; } if(count%4==1) strcpy(time,tmpbuf); if(count%4==2) strcpy(u,tmpbuf); if(count%4==3) strcpy(s,tmpbuf); if(count%4==0){ strcpy(f,tmpbuf); query=tmp; memset ( query,0,sizeof ( query ) ); sprintf(query,"insert into %s(Time,SessionName,FileName)values('%s','%s','%s')",table1,time,s,f); t=mysql_real_query(&mysql,query,(unsigned int)strlen(query)); if(t) { printf("Error making query:%s\n",mysql_error(&mysql)); } else printf("[%s] made ...\n",query); query=tmp; memset(query,0,sizeof(query)); sprintf(query,"insert into %s(Time,URL,TraType,AppType,SessionName)values('%s','%s','TCP','HTTP','%s')",table2,time,u,s); q=mysql_real_query(&mysql,query,(unsigned int)strlen(query)); if(q) { printf("Error making query:%s\n",mysql_error(&mysql)); } else printf("[%s] made ...\n",query); } } mysql_close(&mysql); if(q==0&&t==0) flag=1; if(flag==1) if(truncate("/sqlinformation",0)!=0) printf("truncate error\n"); fclose(fd); return 0;}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -