I'm in the middle of a project using a mysql database and spanning several languages. A problem has arisen that we need a piece of it to be in C, that reads in a query, replaces some placeholders (wrapped in '?' chars), and then runs said query.
The problem here is, C is not one of my strengths In fact, I'm almost illiterate in it. This is what I have so far, its poorly put together and doesnt even work. What it should do, is replace "?value?" with the value of repl_val.
#include <stdio.h>
#include <string.h>
int main ()
{
char* query = "SELECT ?value? FROM Table";
char newquery[100];
char* repl_val = "VariableFieldName";
char* pch;
int start,end;
int count;
// get start and end indices...
pch=strchr(query,'?');
if (pch!=NULL)
{
start = pch-query;
pch=strchr(pch+1,'?');
end = pch-query;
}
// ...or return
if(!start || !end)
{
return 1;
}
// copy everything before first '?', concat the replacement value
strncpy(newquery,query,start);
strcat(newquery,repl_val);
// concat everything after the second '?', one char at a time
for(count=end; count<strlen(query); count++)
{
strcat(newquery,query[count]);
}
printf("new query: %s", newquery);
return 0;
}
The last for() loop wont compile, and even if i comment it out, all that prints is the repl_val surrounded by garbage. Am I just going about this all wrong?