【问题标题】:Why does Metakit not store string values after a commit?为什么 Metakit 在提交后不存储字符串值?
【发布时间】:2017-08-13 15:19:14
【问题描述】:

我正在为 Raspberry Pi 3 开发一个嵌入式应用程序,它将其数据存储在 Metakit 2.4.9.7 数据库中。其中一张表由两列组成(数据类型:双精度+字符串)。双精度值的存储没有任何问题,但是一旦我在数据库上调用 Commit() 方法,字符串值显然会被丢弃。请参阅此代码:

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

第一个循环为我提供了之前填充数据的表的完整内容:列 #1(双精度)和 #2(字符串),而在 Commit() 调用之后运行的第二个循环只会给出以下内容#1,好像 #2 永远不会被填充一样。

这里有什么问题?非常感谢任何有用的提示。

【问题讨论】:

    标签: c++ raspberry-pi3 embedded-database


    【解决方案1】:

    浏览 Metakit Google 群组后,我自己得到了一个提示。正是这行代码,特别是定义字符串中括号内的内容,造成了麻烦:

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

    在 2008 年 3 月,已经有用户抱怨列定义中冒号后面的空格导致数据损坏,但在我的例子中,它是在逗号分隔列定义后的空格。 API 文档中对此没有任何提示!删除空白空间并重建数据库后,一切都突然正常了。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多