converting from int to const char* for mysql Client

25 Apr 2012

So I want to take the value from a digital in or adc port, and send it to a mysql database, using the MySQL Client in the cookbook.

However whenever I try and add my digital pin variable to the code, it tells me "argument is incompatible with corresponding format string conversion"

this is my input:

DigitalIn in7(p7);

and this is the sql code (var1 was the example value to output to sql)

SQL Command Make command char cmd[128] = {0}; const char* var1="Hello World!"; sprintf(cmd, "INSERT INTO test (test) VALUES('%d')", in7);

Anyone have any suggestions

Thanks!

26 Apr 2012

Although the shortcut of calling int value = in7; works when using a direct assignment it wont work when passing it as an argument as it is not an int its a class, for this you must call directly the read() method, also dont use sprintf its dangerous as it has no idea how big the array you pass in is, use snprintf to avoid buffer overflows

snprintf(cmd, 128, "INSERT INTO test (test) VALUES('%d')", in7.read());
26 Apr 2012

That worked perfectly, thanks for the help!