Skip to content

Too many sql statements and it fails #93

Open
@zaplakkies

Description

@zaplakkies

I'm trying to create a routine that initializes the database if we want to do a factory reset. When I run the commands in sequence like this the last few table creates do not happen and subsequent inserts fail with the table does not exist. Ive tried big delays between statements but I still get the error. Swapping the order only changes which ones don't work anymore.
If I run each statement separately from the sql.html sample in the example directly, all the statements pass without issue.

Please can someone help me see where I'm going wrong or how to split it up so I can get it working.

String simplesql(const char *sql)
{
  String resp = "";
  sqlite3_stmt *res;
  const char *tail;
  int rc = sqlite3_prepare_v2(db1, sql, -1, &res, &tail);
  if (rc != SQLITE_OK)
  {
    resp += "Error executing query: ";
    resp += sqlite3_errmsg(db1);
    return resp;
  }

  int rec_count = 0;
  while (sqlite3_step(res) == SQLITE_ROW)
  {
    int count = sqlite3_column_count(res);
    for (int i = 0; i < count; i++)
    {
      const char *col_text = (const char *)sqlite3_column_text(res, i);
      if (col_text)
      {
        resp += col_text;
        if (i < count - 1)
        {
          resp += ", ";
        }
      }
    }
    resp += "\n";
    rec_count++;
  }

  sqlite3_finalize(res);

  if (rec_count == 0)
  {
    resp += "No records found.";
  }

  return resp;
}
String initdb() {
  Serial.println("Initializing database");

  if (openDb("/spiffs/data.db") != SQLITE_OK)
  {
    return "Error opening database";
  }
  const char *initCommands[] = {
    "DROP TABLE IF EXISTS idpoints",
    "DROP TABLE IF EXISTS pointtype",
    "DROP TABLE IF EXISTS pointsclocked",
    "DROP TABLE IF EXISTS eventlog",
    "DROP TABLE IF EXISTS wifisettings",
    "DROP TABLE IF EXISTS audittrail",
    "DROP TABLE IF EXISTS settings",
    "DROP TABLE IF EXISTS eventtype",
    "CREATE TABLE wifisettings (apname TEXT, password TEXT, ip TEXT)",
    "CREATE TABLE idpoints (id INTEGER, value TEXT, name TEXT, lastclocked INTEGER, type INTEGER)",
    "CREATE TABLE pointtype (id INTEGER, name TEXT)",
    "CREATE TABLE pointsclocked (datetime INTEGER, idpoint INTEGER)",
    "CREATE TABLE eventlog (id INTEGER, datetime INTEGER, event INTEGER, data TEXT, sent INTEGER)",
    "CREATE TABLE audittrail (id INTEGER PRIMARY KEY, datetime INTEGER, event TEXT)",
    "CREATE TABLE settings (name TEXT, value TEXT)",
    "CREATE TABLE eventtype (id INTEGER, shortform TEXT, name TEXT)",
    "INSERT INTO pointtype VALUES (1,'Fixed')",
    "INSERT INTO pointtype VALUES (2,'User')",
    "INSERT INTO pointtype VALUES (3,'Asset')",
    "INSERT INTO settings VALUES ('APpass','')",
    "INSERT INTO settings VALUES ('ntp','pool.ntp.org')",
    "INSERT INTO settings VALUES ('gmtoffset','7200')", // TODO: test negative values
    "INSERT INTO eventtype VALUES (1,'PU','Power up')",
    "INSERT INTO eventtype VALUES (2,'PD','Power down')",
    "INSERT INTO eventtype VALUES (3,'CN','Charge On')",
    "INSERT INTO eventtype VALUES (4,'CF','Charge Off')",
    "INSERT INTO eventtype VALUES (5,'X','Inactivity')",
    "INSERT INTO eventtype VALUES (6,'ST','Unit Settings')",
    "INSERT INTO eventtype VALUES (7,'C','Tamper')",
    "INSERT INTO eventtype VALUES (8,'c','Tamper restore')",
    "INSERT INTO eventtype VALUES (9,'D','Touch port short')",
    "INSERT INTO eventtype VALUES (10,'G','Baton date and time set')",
    "INSERT INTO eventtype VALUES (11,'N','Battery OK')",
    "INSERT INTO eventtype VALUES (12,'n','Battery low')",
    "INSERT INTO eventtype VALUES (13,'S','ID Point')",
    "INSERT INTO eventtype VALUES (14,'CW','Tamper Moisture')",
    "INSERT INTO eventtype VALUES (15,'CT','Tamper over temperature')"
  };

  for (auto &cmd : initCommands)
  {
    Serial.println(simplesql(cmd));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions