Search code examples
c++raspberry-pi3embedded-database

Why does Metakit not store string values after a commit?


I'm developing an embedded application for a Raspberry Pi 3 which stores its data in a Metakit 2.4.9.7 database. One of the tables consists of two columns (data types: double + string). The double values are stored without any problems, but the string values are apparently discarded once I call the Commit() method on the database. See this code:

#include <iostream>
#include <map>
#include <mk4.h>
#include "database.h"

using namespace std;

database::database (string file_name)
: connection_handler (file_name.c_str (), 1),
  table_tracks       (connection_handler.GetAs ("tracks [degrees:D, compass:S]"))
{
  cout << "Opening database " << file_name << "." << endl;

  if (table_tracks.GetSize () == 0) {
    c4_DoubleProp degrees ("degrees");
    c4_StringProp compass ("compass");
    c4_Row        track;
    int           i;
    string        compass_points [17] = {"N", "NNE", "NE", "ENE",
                                         "E", "ESE", "SE", "SSE",
                                         "S", "SSW", "SW", "WSW",
                                         "W", "WNW", "NW", "NNW",
                                         "N"};

    cout << "Database is vanilla. Setting it up now." << endl;

    for (i = 0; i <= 16; i++) {
      degrees (track) = 22.5 * (double) i;
      compass (track) = (compass_points [i]).c_str ();

      table_tracks.Add (track);
    }

    for (i = 0; i < table_tracks.GetSize (); i++) {
      cout << (double) degrees (table_tracks [i]) << " " << (string) compass (table_tracks [i]) << endl;
    }

    connection_handler.Commit ();

    for (i = 0; i < table_tracks.GetSize (); i++) {
      cout << (double) degrees (table_tracks [i]) << " " << (string) compass (table_tracks [i]) << endl;
    }
  }
}

The first loop gives me the complete contents of the table previously filled with data: column #1 (double) and #2 (string), whereas the second loop run just after the Commit() call would only give the contents of #1, as if #2 would have never been populated.

What's wrong here? Any useful hints are greatly appreciated.


Solution

  • After browsing the Metakit Google group, I got a hint myself. It was this line of code, particularly, the stuff within the brackets in the definition string, which made trouble:

    connection_handler.GetAs ("tracks [degrees:D, compass:S]");
    

    There has already been a user's complaint in March 2008 about a blank space after the colon in the column definition causing data corrpution, but in my case, it was a blank after a comma separating the column definitions. There was no hint on this in the API docs! After removing the blank space and rebuilding the database, everything suddenly worked fine. :)