SQLite Forum

Passing variable to SQLite3 fields
Login
Hi
I am using SQLite with an ESP32 and using the Arduino IDE

My question is how do I pass the value of string and integer to a field on an SqLite3 table so I can enter a new record.

The example below generates both a number and also a string but I cannot find a way to record these values. The reading value should be stored in readvalue field  and the output of statusStr should be stored in the status field.  I

Any help will be greatly appreciated. 
Thank you in advance

Chazza


void insertrecord() {

int reading = random(0, 1023);
String statusStr = "";
if (reading >= 750) {
statusStr = "HIGH";
} else if (reading <= 749 and reading >= 500) {
statusStr = "MEDIUM";
} else if (reading <= 499 and reading >= 0) {
statusStr = "LOW";
}

Serial.print("AO reading is : "); Serial.println(reading);
Serial.print("Status is : "); Serial.println(statusStr);

// insert values

sqlite3 *db1;
  int rc ;
  
if (db_open("/spiffs/test.db", &db1))
    return;

Serial.println("Inserting new record");

// line below to add new record with default values when button pushed for testing this works fine
rc = db_exec(db1, "INSERT INTO aoreadings VALUES (1, 2, 'abcdefg');");

// line below add new record with value 1 in id field, reading in readvalue field and StatusStr into status field when button pushed. This does not work

rc = db_exec(db1, "INSERT INTO aoreadings VALUES (id, readvalue, status);(1, reading, statusStr)");

if (rc != SQLITE_OK) {
sqlite3_close(db1);
return;
}

}