Arduino sensor data to MYSQL table

Hello,

I have this working code to write some data to MYSQL remote server:

#include <MySQL_Cursor.h>

/*
MySQL Connector/Arduino Example : connect by hostname

This example demonstrates how to connect to a MySQL server resolving the
hostname for cases when you do not know the IP address of the server or
it changes because it is in the cloud.

INSTRUCTIONS FOR USE

  1. Change the hostname variable to the hostname of the MySQL server
  2. Change the user and password to a valid MySQL user and password
  3. Connect a USB cable to your Arduino
  4. Select the correct board and port
  5. Compile and upload the sketch to your Arduino
  6. Once uploaded, open Serial Monitor (use 115200 speed) and observe

If you do not see messages indicating you have a connection, refer to the
manual for troubleshooting tips. The most common issues are the server is
not accessible from the network or the user name and password is incorrect.

Note: The MAC address can be anything so long as it is unique on your network.

Created by: Dr. Charles A. Bell
*/
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <Dns.h>

int analogPin = 0; // potentiometer wiper (middle terminal) connected to analog pin 3
int val = 0; // variable to store the value read

byte mac_addr = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char hostname = “gator4080.hostgator.com”; // change to your server’s hostname/URL
char user = “”; // MySQL user login username
char password = “”; // MySQL user login password

// Sample query

char INSERT_SQL = “UPDATE serveris_scada.k63esami SET rod1=555”;

IPAddress server_ip;
EthernetClient client;
MySQL_Connection conn((Client *)&client);
DNSClient dns_client; // DNS instance

void setup() {
val = analogRead(analogPin);
Serial.begin(115200);
while (!Serial); // wait for serial port to connect
Ethernet.begin(mac_addr);
// Begin DNS lookup
dns_client.begin(Ethernet.dnsServerIP());
dns_client.getHostByName(hostname, server_ip);
Serial.println(server_ip);
// End DNS lookup
Serial.println(“Connecting…”);
if (conn.connect(server_ip, 3306, user, password)) {
delay(1000);
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
val = analogRead(analogPin); // read the input pin
Serial.println(val);
cur_mem->execute(INSERT_SQL);

// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}
else
Serial.println(“Connection failed.”);

}

void loop() {
delay(2000);
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
Serial.println(“Recording data.”);

// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}

Problem is that I can’t write INTEGER (int val) is my readAnalog(sensor) to MYSQL table:

“UPDATE serveris_scada.k63esami SET rod1=val”;

If I use this command:

“UPDATE serveris_scada.k63esami SET rod1=555”;

555 value writes , OK.

Question, how I can write Integer to MYSQL table ?

Thank you.

Problem is that I can't write INTEGER (int val) is my readAnalog(sensor) to MYSQL table:

Why can't you?

"UPDATE serveris_scada.k63esami SET rod1=val";

The quotes make that a string. The value stored in val will not be used in place of the name.

char stuff[80];
sprintf(stuff, "UPDATE serveris_scada.k63esami SET rod1=%d", val);

Then, use stuff in place of INSERT_SQL.