siara-cc / esp32_arduino_sqlite3_lib

Sqlite3 Arduino library for ESP32
Apache License 2.0
350 stars 66 forks source link

how to reopen connection database after close to be used again? #9

Closed siara-cc closed 5 years ago

siara-cc commented 5 years ago

how to reopen connection database after close to be used again?

siara-cc commented 5 years ago

Hi, sorry I am unable to understand what the problem is.. Is it possible to give a code example?

unangwarsodi commented 5 years ago

this example script :

include

include

include

include

include

include "SD_MMC.h"

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; }

void setup() { Serial.begin(115200); sqlite3 db1; sqlite3 db2; char *zErrMsg = 0; int rc;

SPI.begin(); SD_MMC.begin();

File root = SD_MMC.open("/"); if (!root) { Serial.println("- failed to open directory"); delay(2000); ESP.restart(); 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(); }

// SD_MMC.remove("/test1.db");

sqlite3_initialize();

if (openDb("/sdcard/test1.db", &db1)) return;

rc = db_exec(db1, "CREATE TABLE test1 (id INTEGER, content);"); if (rc != SQLITE_OK) { sqlite3_close(db1); return; }

rc = db_exec(db1, "INSERT INTO test1 VALUES (1, 'Hello, World from test1');"); if (rc != SQLITE_OK) { sqlite3_close(db1); return; }

//CLOSED sqlite3_close(db1);

//REUSE rc = db_exec(db1, "INSERT INTO test1 VALUES (2, 'Hello, World from test1');"); if (rc != SQLITE_OK) { sqlite3_close(db1); return; }

rc = db_exec(db1, "SELECT * FROM test1"); if (rc != SQLITE_OK) { sqlite3_close(db1); return; }

sqlite3_close(db1);

}

void loop() { }

siara-cc commented 5 years ago

I think your problem will go away if you open it again before inserting the second row:

if (openDb("/sdcard/test1.db", &db1))
return;
unangwarsodi commented 5 years ago

I try open again and it issued an error out of memory

siara-cc commented 5 years ago

Pls see the other thread to find out why it shows out of memory and how to overcome the problem.