I have a table with 6 columns and i have CSV Export like this with 6 entries per Line
me;val1;val2;val3;val4;val5;
me;val1;val2;val3;val4;val5;
I need to read these entries and Insert it into the SQLITE3 Database.So inorder to parse the CSV I use
void readcsv()
{
FILE* stream = fopen("input.csv", "r");
char line[1024];
while (fgets(line, 1024, stream))
{
char* tmp = strdup(line);
printf("Field 3 would be %s\n", getcsvfield(tmp, 3));
// NOTE strtok clobbers tmp
free(tmp);
}
}
//Used for parsing CSV
const char* getcsvfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, ";\n"))
{
if (!--num)
return tok;
}
return NULL;
}
So will i have to call getcsvfield(tmp, fieldnumber) again and again in the Insert Query like this or is there a cleaner and efficient way.
execute("INSERT INTO table(a,b,c,d,e,f) VALUES('%s','%s','%s');",getcsvfield(tmp, 1),getcsvfield(tmp, 2),....... and so on
Please advise.
Aucun commentaire:
Enregistrer un commentaire