#include <sqlite3.h>
#include <SPIFFS.h>
/* You only need to format SPIFFS the first time you run a
test or else use the SPIFFS plugin to create a partition
https://github.com/me-no-dev/arduino-esp32fs-plugin */
#define FORMAT_SPIFFS_IF_FAILED true
const char* data = "Callback function called";
static int callback(void *data, int argc, char **argv, char **azColName) {
int i;
Serial.printf("%s: ", (const char*)data);
for (i = 0; i<argc; i++){
Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
Serial.printf("\n");
return 0;
}
int openDb(const char *filename, sqlite3 **db) {
int rc = sqlite3_open(filename, db);
if (rc) {
Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db));
return rc;
} else {
Serial.printf("Opened database successfully\n");
}
return rc;
}
char *zErrMsg = 0;
int db_exec(sqlite3 *db, const char *sql) {
Serial.println(sql);
long start = micros();
int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if (rc != SQLITE_OK) {
Serial.printf("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
Serial.printf("Operation done successfully\n");
}
Serial.print(F("Time taken:"));
Serial.println(micros()-start);
return rc;
}
int db_open(const char *filename, sqlite3 **db) {
int rc = sqlite3_open(filename, db);
if (rc) {
Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db));
return rc;
} else {
Serial.printf("Opened database successfully\n");
}
return rc;
}
void setup() {
Serial.begin(115200);
sqlite3 *db1;
sqlite3 *db2;
int rc;
if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
Serial.println("Failed to mount file system");
return;
}
// list SPIFFS contents
File root = SPIFFS.open("/");
if (!root) {
Serial.println("- failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
// remove existing file
SPIFFS.remove("/esp32s.db");
sqlite3_initialize();
if (db_open("/spiffs/esp32s.db", &db1)){
return;
}
rc = db_exec(db1, "CREATE TABLE coordenadas (id INTEGER PRIMARY KEY, longitud VARCHAR(100), latitud VARCHAR(100), altitud VARCHAR(100), fecha DATETIME);");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
return;
}
rc = db_exec(db1, "INSERT INTO coordenadas VALUES (0, 1.54,2.56,3.45,'2008-11-01 10:00:00');");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
sqlite3_close(db2);
return;
}
rc = db_exec(db2, "SELECT * FROM coordenadas WHERE id = 1;");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
sqlite3_close(db2);
return;
}
rc = db_exec(db2, "DELETE FROM coordenadas WHERE id = 1;");
if (rc != SQLITE_OK) {
sqlite3_close(db1);
sqlite3_close(db2);
return;
}
}
void loop() {
// put your main code here, to run repeatedly:
}