Hello All,
I've been working on a program which writes to MySQL. It needs some variables posting into the database and I'm having trouble elegantly constructing a char * string to send to the MySQL connector.
Here's what I'm doing at present, apologies for my awful code, it works but seems unnecessarily long-winded and ugly. Does anyone know of a more elegant way to insert variables into a char * string? If there were a need for lots of SQL queries this method could get ugly very quickly!
Thanks for any pointers (heehee)Code:char *arg1a = "select * from database where field1 = '"; char *arg1b = "' and field2 = '"; char *arg1c = "'"; char *sqlarg; sqlarg = (char *)malloc((strlen(variable1) + strlen(variable2) + strlen(arg1a) + strlen(arg1b) + strlen(arg1c) + 1) *sizeof(char)); strcpy(arg1, arg1a); strcat(arg1, variable1); strcat(arg1, arg1b); strcat(arg1, variable2); strcat(arg1, arg1c); /* completed query now stored in sqlarg */



LinkBack URL
About LinkBacks


