I actually want to execute an SQL query in a C program, with the variables in a SELECT query to be the value stored in a string variable.
For example:
void fetch_data(char var[])
{
char COL1[]=var, COL2[]="Address", COL3[]="Name";
SELECT COL1, COL2, COL3 FROM TABLE WHERE COL4='some value';
}
Here as you can see I want want my code to be flexible so that I can have different column names depending on the variable var which is the parameter of the fetch_data function.
Please tell me if this is possible in C.
I have thought of another method if the above is not possible: can we store the whole SQL statement in a string and execute it, so that I can modify this string whenever I want according to the parameter's value that I get in the function fetch_data()?
The code below will make my point more clear that what I want:
void fetch_data(char var[])
{
char COL1[]="Name", COL2[]="Address", COL3[]=var;
char qry1[]="SELECT ", qry2[]=var, qry3=" COL2, COL3 FROM TABLE WHERE COL4='some value';";
char str[]=strcat(qry1,qry2);
char query[]=strcat(str,qry3);
//now query will be having "select (value of var), COL2, COL3 FROM TABLE WHERE COL4='some value';
}
Now in the above code, can I execute the query that is stored in the string query?
Please let me know if any of the 2 methods can work or if it can be achieved by any other method in 'C'.