increment of variable not possible Can you spot the fault??

I am stuck on this code, I want to increment a variable data1 then typecast int to string, replace in main string, and finally convert to char array.

Running this code shows the variable is not incrementing and I fail to understand why.

char INSERT_DATA1[200] = {""};
String normalString = "INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')";
int data1 = 10;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
     data1++;                                       //increment the data variable to put to replace d1 
     String d1 = (String)data1;                     //int to string with typecast
     normalString.replace("d1",d1);                 //replace d1 in normal string 
     normalString.toCharArray(INSERT_DATA1,200);    //convert normal string to char array
     
     Serial.print("INSERT_DATA1 = ");               //print out the char array    
     for (int i = 0 ; i < 200 ; i++)                //
       {                                            //
       Serial.print(INSERT_DATA1[i]);               //
       }                                            //  
       Serial.println();                            //
} 

/*__________ON NEXT LOOP THE data1 VARIABLE IS NOT INCREMENTED PLEASE HELP ME UNDERSTAND WHY___________________*/
/*                         --------           -----------------

SerialMonitor show this :             scroll right to see variable d1 to d8.  '11' should be incrementing. 
---------------------------
INSERT_DATA1 = INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, '11', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')
INSERT_DATA1 = INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, '11', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')
INSERT_DATA1 = INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, '11', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')
INSERT_DATA1 = INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, '11', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')
INSERT_DATA1 = INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, '11', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')
INSERT_DATA1 = INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, '11', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')
     data1++;                                       //increment the data variable to put to replace d1 
     String d1 = (String)data1;                     //int to string with typecast

Casting is not converting.

Grumpy_Mike:     data1++;                                      //increment the data variable to put to replace d1     String d1 = (String)data1;                    //int to string with typecast

Casting is not converting.

Hi Thanks for reply. Can you please explain more The problem is data1 beeing reset to 10 on each loop. the typecast and string stuff seems to be working. When setting the data manually, the value is converted corectly from int and all the way out to charArray.

http://www.tutorialspoint.com/cprogramming/c_type_casting.htm

is not:-

http://stackoverflow.com/questions/9655202/how-to-convert-integer-to-string-in-c

Grumpy_Mike: http://www.tutorialspoint.com/cprogramming/c_type_casting.htm

is not:-

http://stackoverflow.com/questions/9655202/how-to-convert-integer-to-string-in-c

Thank you I guess..., You are certanly a man of few words.

Can anyone please explain what Grumpy_Mike is trying to say???

If you can make a simple sketch that fits into the width of the screen to demo what you are trying to do I may be able to help.

...R

normalString.replace("d1",d1);                 //replace d1 in normal string

You have :-

'd1'

'd1' is not the same as "d1"

The problem is that the substring ‘d1’ is replaced with 11 on the first pass, so the string is changed from ‘d1’ to ‘11’, but your code looks for ‘d1’ on all subsequent passes. This demonstrates the problem:

#include <String.h>
char INSERT_DATA1[200] = {""};
String normalString = "INSERT INTO `HelgeNodland`.`Data1` (`Lines`, `Time`, `Radon`, `P1`, `P2`, `RPM`, `T1`, `T2`, `Wvoid`, `Serial`) VALUES (NULL, CURRENT_TIMESTAMP, 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8')";
int data1 = 10;
String oldD1;

void setup()
{
  Serial.begin(115200);
  oldD1 = "d1";
}

void loop()
{
   data1++;                                       //increment the data variable to put to replace d1 
   String d1 = (String)data1;                     //int to string with typecast
   normalString.replace(oldD1,d1);                //replace d1 in normal string 
   oldD1 = d1;
   normalString.toCharArray(INSERT_DATA1,200);    //convert normal string to char array
   Serial.println(normalString.substring(140, 160));
                     
}

econjack:
The problem is that the substring ‘d1’ is replaced with 11 on the first pass, so the string is changed from ‘d1’ to ‘11’, but your code looks for ‘d1’ on all subsequent passes. This demonstrates the problem:

#include <String.h>

char INSERT_DATA1[200] = {""};
String normalString = “INSERT INTO HelgeNodland.Data1 (Lines, Time, Radon, P1, P2, RPM, T1, T2, Wvoid, Serial) VALUES (NULL, CURRENT_TIMESTAMP, ‘d1’, ‘d2’, ‘d3’, ‘d4’, ‘d5’, ‘d6’, ‘d7’, ‘d8’)”;
int data1 = 10;
String oldD1;

void setup()
{
  Serial.begin(115200);
  oldD1 = “d1”;
}

void loop()
{
  data1++;                                      //increment the data variable to put to replace d1
  String d1 = (String)data1;                    //int to string with typecast
  normalString.replace(oldD1,d1);                //replace d1 in normal string
  oldD1 = d1;
  normalString.toCharArray(INSERT_DATA1,200);    //convert normal string to char array
  Serial.println(normalString.substring(140, 160));
                   
}

AMP, ‘2247’, ‘d2’, ’
AMP, ‘2248’, ‘d2’, ’
AMP, ‘2249’, ‘d2’, ’
AMP, ‘2250’, ‘d2’, ’
AMP, ‘2251’, ‘d2’, ’
AMP, ‘2252’, ‘d2’, ’
AMP, ‘2253’, ‘d2’, ’
AMP, ‘2254’, ‘d2’, ’
AMP, ‘2255’, ‘d2’, ’

And it works :slight_smile:

I see now what was wrong. Thank you for your time and great posts.