【问题标题】:How to create a mnesia table with one column & read/write from/to there?如何创建一个包含一列的 mnesia 表并从那里读/写?
【发布时间】:2014-02-18 12:52:08
【问题描述】:
-record(ng, {ng}).

mnesia:create_table(ng, [{type, set}, {attributes, record_info(fields, ng)}]).

我收到:{aborted,{bad_type,ng,{attributes,[ng]}}} 错误。

怎么了?如何创建一个包含一列(已命名)的 mnesia 表?

【问题讨论】:

    标签: erlang mnesia


    【解决方案1】:

    记录必须至少有 2 个字段。这会起作用:

    -record(ng, {ng, extrafield}).
    mnesia:create_table(ng, [{type, set}, {attributes, record_info(fields, ng)}]).
    

    来自http://www.erlang.org/doc/man/mnesia.html#create_table-2

    “表必须至少有一个额外的属性除了键。”

    编辑:无法找到关于单列是否可能的答案,但this 2007 thread 表示不可能。

    我个人用键/值列来做,像这样:

    -record(proximaglobal, {key, value}).
    mnesia:create_table(proximaglobal, [{attributes, record_info(fields, proximaglobal)}, {disc_only_copies, [node()]}]).
    mnesia:sync_transaction(fun() -> mnesia:write(#proximaglobal{key=time, value=WorldTime}) end).
    mnesia:sync_transaction(fun() -> mnesia:read(proximaglobal, time) end).
    

    【讨论】:

      【解决方案2】:

      {attributes, AtomList} 应该填充表的记录的属性名称列表。默认值为 [key, val]。除了键之外,该表还必须至少具有一个额外的属性。

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-03
        • 2020-05-28
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多