hello together,
I am really struggeling with json right now. I have a callback function in which a JSON array is beeing filled each time its executed. In a different function I print the serialized JSON document with the array. However, after about 7 iterations (depending on the siue of the array items, the print out becomes empty:
{"response":[{"profile_id":"1","profile_name":"vader"},{"profile_id":"2","profile_name":"luke"},{"profile_id":"3","profile_name":null}],"code":1}
this part obove is the correct content
Opened database successfully
Operation done successfully
Time taken:172 ms
{"response":[],"code":2}
this part obove is missing the array content
Opened database successfully
Operation done successfully
Time taken:170 ms
{"response":[],"code":0}
here is some extraction of my code:
String buffering;
StaticJsonDocument < 4096 > doc;
JsonArray daten = doc.createNestedArray("response");
//-----------------DB Enquery-----------------------
void db_enquery(String enquery) {
buffering="";
doc["code"] = random(0,3);
for(int i=daten.size();i>=0;--i)daten.remove(i);
db_open();
db_exec(enquery.c_str());
serializeJson(doc,Serial);
serializeJson(doc,buffering);
if (db_file_name[0] != 0) {
db_file_name[0] = 0;
sqlite3_close(db);
}
}
//-----------------DB Enquery-----------------------
//-----------------mein Callback for sql enqueries----------------------
static int callback(void * data, int argc, char * *argv, char * *azColName) {
StaticJsonDocument<1800> docx;
JsonObject sub = docx.createNestedObject();
//Serial.println(String(counter));
for (int i = 0; i < argc; i++) {
sub[azColName[i]] = String(argv[i]);
}
daten.add(sub);
counter++;
return 0;
}
//-----------------mein Callback for sql enqueries----------------------
//-----------------open Database----------------------
int db_open() {
if (db != NULL) sqlite3_close(db);
int rc = sqlite3_open(DATABASE_NAME, &db);
if (rc) {
Serial.print(F("Can't open database: "));
Serial.print(sqlite3_extended_errcode(db));
Serial.print(" ");
Serial.println(sqlite3_errmsg(db));
return rc;
} else Serial.println(F("Opened database successfully"));
return rc;
}
char * zErrMsg = 0;
const char * data = "Output:";
int db_exec(const char * sql) {
counter = 0;
if (db == NULL) {
Serial.println("No database open");
return 0;
}
first_time = true;
long start = micros();
int rc = sqlite3_exec(db, sql, callback, (void * ) data, &zErrMsg);
//serializeJson(doc, Serial);
//Serial.println("\r\n");
if (rc != SQLITE_OK) {
Serial.print(F("SQL error: "));
Serial.print(sqlite3_extended_errcode(db));
Serial.print(" ");
Serial.println(zErrMsg);
sqlite3_free(zErrMsg);
} else {
Serial.println(F("Operation done successfully"));
}
Serial.print(F("Time taken:"));
Serial.print((micros() - start) / 1000);
Serial.println(F(" ms"));
return rc;
}