0

I had following setup earlier:

    String INSERT_SQL = "";
    // inserting to sql database on mysql server
    INSERT_SQL = "";
    INSERT_SQL.concat("INSERT INTO arduinoSensorData.sensorLog (out_temperature, out_humidity, ");
    INSERT_SQL.concat(" drwngRoom_temperature, drwngRoom_humidity, pot1_soilMoisture, pot1_avg_SoilMoisture,");
    INSERT_SQL.concat(" wateringPot1) VALUES ('");
    INSERT_SQL.concat(outdoorTempInC);
    INSERT_SQL.concat("', '");
    INSERT_SQL.concat(outoorHumidity);
    INSERT_SQL.concat("', '");
    INSERT_SQL.concat(indorTempinC);
    INSERT_SQL.concat("', '");
    INSERT_SQL.concat(h);
    INSERT_SQL.concat("', '");
    INSERT_SQL.concat(val);
    INSERT_SQL.concat("', '");
    INSERT_SQL.concat(avgVal);
    INSERT_SQL.concat("', '");
    if (wateringBasedOnAlarm){
      waterMsg = "water based on alarm";

    } else waterMsg = soilMsg;
    INSERT_SQL.concat(waterMsg);
    INSERT_SQL.concat("');");   

And later, I was converting it to char like this:

const char *mycharp = INSERT_SQL.c_str();

Which I realised wrong approach as in every iteration of the loop function of arduino MCU this will consume more memory!

So, I decided to change INSERT_SQL to char instead of declaring it as String at first place.

I initialised INSERT_SQL as char like this:

char INSERT_SQL[295];

Because I realised the total length would not be more than 295,

But when compiler reaches the line:

INSERT_SQL = "";

I got an error saying:

incompatible types in assignment of 'const char [1]' to 'char [295]'

So, this is the first thing I need is to fix it and secondly, I think I cannot use concat with char datatype, so in that case how do I populate values to sql query using sprintf?

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

0

Compiler is exactly telling you what's wrong.

char INSERT_SQL[295];
INSERT_SQL = "";

You probably want to do like:

char INSERT_SQL[295];
INSERT_SQL[0] = 0x00;
::strcat(INSERT_SQL, "INSERT INTO....
JazzSoft
  • 392
  • 2
  • 11
  • Using `::strcat(INSERT_SQL, "INSERT INTO....` what namespace `strcat` would it be using? Do I not have to be explicit? – Ciasto piekarz Jan 25 '17 at 19:52
  • strcat() is a standard C library function defined in string.h. All C functions are in the global name scope. You don't have to add "::" at the beginning, if you don't want. – JazzSoft Jan 25 '17 at 19:56