jwhiddon / EDB

A re-implementation of the Arduino database library to allow more than 256 records
GNU Lesser General Public License v2.1
89 stars 43 forks source link

db.open() not working? #35

Open uvedhe opened 6 years ago

uvedhe commented 6 years ago

I've managed to create a database (which shows up as a file on my SD card), but my program doesn't get past the line EDB_Status result = db.open(0);. Any idea what could be going wrong? Below my code (using an UNO):

#include <SPI.h>
#include <SD.h>
#define SD_PIN 10  //SD read/write pin
#define TABLE_SIZE 8192  //256*32
#include "Arduino.h"
#include <EDB.h>

char* db_name = "/DB/SENSOR.DB";
File dbFile;

struct sensorLog {
  unsigned int id;
  float input;
  unsigned int timestamp;    
} 
sensorLog;

// Setting up EDB object
inline void writer (unsigned long address, const byte* data, unsigned int recsize) {
  dbFile.seek(address);
  dbFile.write(data,recsize);
  dbFile.flush();
}
inline void reader (unsigned long address, byte* data, unsigned int recsize) {
  dbFile.seek(address);
  dbFile.read(data,recsize);
}
// Create an EDB object with the appropriate write and read handlers
EDB db(&writer, &reader);

void setup() {

  Serial.begin(57600);

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_PIN)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // create file
  dbFile = SD.open(db_name, FILE_WRITE);
  db.create(0, TABLE_SIZE, (unsigned int)sizeof(sensorLog));
  dbFile.close();

  // open file
  dbFile = SD.open(db_name, FILE_WRITE);
  if (!dbFile) { Serial.println("File does not exist"); }
  if (dbFile) { 
    Serial.println("File exists!");  // returns "File exists!"
    EDB_Status result = db.open(0);
    Serial.println("Result: " + String(result));  // is not printed
    if (result == EDB_OK) {
        Serial.println("DONE");
    } else {
        Serial.println("ERROR: Did not find database in the file " + String(db_name));
    }
  }

  // code to do stuff, if the above would work...

  // close file
  dbFile.close();

}

void loop() {

}