So I'm trying to build my sql statement in the for loop.
But on executing, it shows error at the very first point of the string.
Is the concatenation method wrong, cause I printed the sql statement and manually execute in postgresql and it works.
Also I had tried
char *sqlStatement = "INSERT INTO record()..VALUES();\
INSERT INTO record()..VALUES();\
INSERT INTO record()..VALUES();
"
That works.
Note that I have reduced the loop to only once and reduced the number of columns for brevity.
The code:
char sqlStatement[8000];
for(int i=0;i<1;i++) {
sprintf(&sqlStatement[0] + strlen(sqlStatement), "INSERT INTO record (\"user_id\", \"filename\", \"ports\", \"timestamp\" ... )VALUES (1, 'example%d', 0, '123456789%d', ... );", i, i,);
}
pgResult = PQexec(pgConn, sqlStatement);
if (PQresultStatus(pgResult) != PGRES_COMMAND_OK) {
printf("%s", PQresultErrorMessage(pgResult));
PQclear(pgResult);
closeSQL(pgConn);
exit(-1);
}
Error message:
ERROR: syntax error at or near ""
LINE 1: INSERT INTO captures ("user_id", "filename", "ports", "time...
^