concatenating and Char data types (headache!)

Hey Guys,

I am working with the MySQL library which required an SQL query to be in the form of a char.

char QUERY_SQL[] = "UPDATE gps_stat.raw_inputs SET snr=snr+1 WHERE id=1";

which basically requests to connect to the database and updates a counter. A bit later in the program the query is executed.

my_conn.cmd_query(QUERY_SQL);

(it likes a char type)

The problem is I would like to throw more then just a static query at it. I would like to have some static text and dynamic data (external sensor) put together to make the query.

I had looked at using string data first with multiple fields all concatenated together to form one big string which then got converted to a char but this didn't work and I suspected it wasn't going to be very memory friendly.

String QUERY_SQL = String("UPDATE gps_stat.raw_inputs SET snr=" + String(DATA[0]) + " WHERE id=1");// concatenating three strings

(DATA[0] is the reference to my sensor data for that particular second)

Does anyone have any suggestions? Can I concatenate multiple char fields into one char field or is there any easier way to go from string to char?

Cheers,

Snip

There are 3 libc functions to handle concatenation of strings.

  • size_t strlcat (char *, const char *, size_t)
  • char * strncat (char *, const char *, size_t)
  • char * strcat (char *, const char *)

Be careful not to overrun your buffers
documentation

I think that sprintf would work as you want to put the data into your char array, as long is its not floats.

Thanks guys, I'll have a muck around with those functions and see how I go.

There are also AVR-specific string functions (usually with the suffix "_P") that allow you concatenate constant strings held in flash memory.

I am working with the MySQL library which required an SQL query to be in the form of a char.

No. It requires the data to be in the form of a char ARRAY. Big difference.