【问题标题】:MatchHead in Mnesia selectMnesia 中的 MatchHead 选择
【发布时间】:2013-04-11 16:09:50
【问题描述】:

我在使用mnesia:select/2 时遇到了一些问题,但我认为它们与 MatchHead 有关。在我的代码中,我有以下记录:

-record(pet_info, {name, id, type}).

我在 Mnesia 表(即 pet_table)中使用此记录放置了一些值。我使用 tv:start()) 检查了它们实际上是否在表中。

我想检索所有 ID,但我的代码总是返回一个空列表。

这是我的代码的突出部分

F = fun() ->
            Pet = #pet_info{id = '$1', _ = '_'},
            mnesia:select(pet_table, [{Pet, [], ['$1']}])
    end,
Reply = case mnesia:transaction(F) of
            {atomic, ResultOfFun} ->
                ResultOfFun;
            {aborted, _Reason} ->
                {error, aborted}
        end,
Reply.

你能告诉我错误在哪里吗?

【问题讨论】:

  • MatchHead 没有问题,因为它对我有用。确保在创建 pet_table 时,{record_name, pet_info} 已在 TabDef 中指定
  • 你能提供一个例子作为答案吗?

标签: erlang mnesia


【解决方案1】:

我找到了解决问题的方法。

表被创建为:

mnesia:create_table(pet_table, [{disc_copies, [node()]},
                                {type, set},
                                {attributes, record_info(fields, pet_info)}])

这没有用。

另一方面,通过将表的名称更改为与它工作的记录相同的名称。

mnesia:create_table(pet_info, [{disc_copies, [node()]},
                                {type, set},
                                {attributes, record_info(fields, pet_info)}])

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 2011-09-05
    • 2013-05-05
    • 2013-03-06
    • 2019-11-22
    • 2019-07-23
    • 2012-02-01
    • 2017-04-21
    • 2014-11-20
    相关资源
    最近更新 更多