I had written a code to read a file and insert it's contents to the database. Since I will receive 3 files every hour, so, this program should read those files and insert the contents accordingly to the database and then after that, those files need to be removed to another folder. This program then will be executed again once it receive another 3 files on the next hour. The reason why I need to remove those files to another folder is becoz, the next 3 files will use the same name as the files before. The filename format will be:
1) DB1-DD-MM-YYYY.txt
2) DB2-DD-MM-YYYY.txt
3) DB3-DD-MM-YYYY.txt
Table structure will be:
mysql> desc PostpaidProfile2;
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| RecordID | int(10) unsigned | | PRI | NULL | auto_increment |
| MobileNumber | varchar(11) | YES | | NULL | |
| Status | tinyint(1) | YES | | 0 | |
| CreateDate | datetime | YES | | NULL | |
| TerminateDate | datetime | YES | | NULL | |
| PackagePlan | varchar(100) | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
If this program read DB1-DD-MM-YYYY.txt, then it will insert all the contents of the file to the PostpaidProfile2 table and in the PackagePlan field, it will indicate "Unlimited" and vice versa for DB2-DD-MM-YYYY.txt which will insert "Pay-Per-Use" in the PackagePlan field.
Now, I need to alter the code so that this program will scan all files in that folder and insert it properly into table as what have been explained above.
Expand|Select|Wrap|Line Numbers
- #include <mysql.h>
- #define HOST "localhost"
- #define USER "yati"
- #define PASSWD "yati"
- #define DB_NAME "subscriberTool"
- void readLogFile(const char* filename);
- void insert2DB();
- void logSeparator(char log[1600]);
- void logSeparatorDetails(char logDetails[2500]);
- void date2DB(char dateLog[20]);
- static MYSQL demo_db;
- char string1[150], msg[250];
- char date[50],time[50],hpno[50];
- char dateLog[20], filename[200];
- char dd[20], mm[20], yyyy[20], theDate[20];
- int main(int argc, char *argv[])
- {
- int i;
- if ( argc == 1 )
- {
- return -1;
- }
- for (i = 1; i < argc; i++)
- {
- readLogFile(argv[i]);
- }
- return 0;
- }
- void readLogFile(const char* filename)
- {
- char line[1600];
- FILE *fp;
- fp=fopen(filename, "r+");
- if(fp==NULL) {
- printf("file not found!\n");
- exit(0);
- }
- else {
- while(!feof(fp)) {
- bzero(line, sizeof(line));
- fgets(line,1600,fp);
- logSeparator(line);
- logSeparatorDetails(string1);
- date2DB(date);
- insert2DB();
- }
- }
- fclose(fp);
- return;
- }
- void insert2DB()
- {
- char query[16384];
- char query1[16384];
- int stat;
- if(!mysql_connect(&demo_db, HOST, USER, PASSWD))
- {
- printf(mysql_error(&demo_db));
- exit(1);
- }
- if(mysql_select_db(&demo_db, DB_NAME))
- {
- printf(mysql_error(&demo_db));
- exit(1);
- }
- bzero(query, sizeof(query1));
- sprintf(query, "INSERT INTO PostpaidProfile2 (MobileNumber,PackagePlan,Status) VALUES('%s','%s',1)",hpno,msg,stat);
- printf("%s\n",query);
- if(mysql_real_query(&demo_db, query, strlen(query)+255))
- {
- printf(mysql_error(&demo_db));
- bzero(query, sizeof(query));
- exit(1);
- }
- mysql_close(&demo_db);
- return;
- }
- void logSeparator(char log[2000])
- {
- char *temp, msgTemp[200];
- int io;
- io=0;
- bzero(string1, sizeof(string1));
- bzero(msg, sizeof(msg));
- temp = strtok (log," ");
- while (temp != NULL)
- {
- io++;
- switch(io)
- {
- case 1:
- strcpy (string1, temp);
- break;
- default:
- strcpy (msgTemp, temp);
- if (strlen(msg) != 0) {
- strcat(msg," ");
- }
- strcat(msg,msgTemp);
- break;
- }
- temp = strtok (NULL," ");
- }
- }
- void logSeparatorDetails(char logDetails[2000])
- {
- char *temp2;
- int io2;
- io2=0;
- bzero(hpno, sizeof(hpno));
- bzero(msg, sizeof(msg));
- temp2 = strtok (logDetails,",");
- while (temp2 != NULL)
- {
- io2++;
- switch(io2)
- {
- case 1:
- strcpy (hpno, temp2);
- break;
- case 2:
- strcpy (msg, temp2);
- break;
- }
- temp2 = strtok (NULL,",");
- }
- }
- void date2DB(char dateLog[20])
- {
- bzero(dd, sizeof(dd));
- bzero(mm, sizeof(mm));
- bzero(yyyy, sizeof(yyyy));
- bzero(theDate, sizeof(theDate));
- strncpy(dd,dateLog,2);
- strncpy(mm,dateLog+2,2);
- strncpy(yyyy,dateLog+4,4);
- strcat(theDate,yyyy);
- strcat(theDate,"-");
- strcat(theDate,mm);
- strcat(theDate,"-");
- strcat(theDate,dd); //convert to yyyy-mm-dd as in mysql date format
- return;
- }
Thanks in advance