【问题标题】:change data between two table in erlang在erlang中的两个表之间更改数据
【发布时间】:2023-04-01 01:55:02
【问题描述】:

我有这张桌子:

 -record(person, {id, firstname, lastname}).
  -record(person_backup, {id, firstname, lastname}).

我创建表 person_backup :

   create_backup()
    mnesia:create_table(person_backup,[{disc_copies, [node()]},{attributes, record_info(fields, person)},
        {record_name, person}]).

我也有这个功能:

create_table_increment()->
    mnesia:create_table(my_auto_inc,[{type,set}]).

我想通过一些修改将数据从 person 传输到 person_backup:

例如,如果表人有:

13  asma   chabani
14  nawel  jirard
15  ahme   bahri

table person_backup 变成了:

1  asma   chabani
2  nawel  jirard
3  ahme   bahri

我尝试使用此代码:

test()->

        Match=#person{_ = '_'},                               %Will match all records
    Fun = fun() ->
              List = mnesia:match_object(Match),
              lists:foreach(fun(X) ->
        NextKey = mnesia:dirty_update_counter(my_auto_inc,lastrec,1),

                              Update = X#person_backup{id=NextKey,firstname = #person.firstname,lastname=#person.lastname},
                                mnesia:write(Update)
                            end, List)
          end,
    mnesia:transaction(Fun).

但是当我测试我有这个错误:

1> model:test().
 {aborted,{combine_error,my_auto_inc,update_counter}} 

我也尝试使用此代码:

test2()->

    Match=#person{_ = '_'},                               %Will match all records
Fun = fun() ->
          List = mnesia:match_object(Match),
          lists:foreach(fun(X) ->
                                NextKey = mnesia:dirty_update_counter(my_auto_inc,lastrec,1),

                           Update = X#person_backup{id=NextKey,firstname = #person.firstname,lastname=#person.lastname},
                            mnesia:write(Update)
                        end, List)
      end,
mnesia:transaction(Fun).

并显示此错误:

model:test2().
{aborted,{{badrecord,person_backup},
          [{model,'-test2/0-fun-0-',1},
           {lists,foreach,2},
           {mnesia_tm,apply_fun,3},
           {mnesia_tm,execute_transaction,5},
           {erl_eval,do_apply,5},
           {shell,exprs,6},
           {shell,eval_exprs,6},
           {shell,eval_loop,3}]}}

【问题讨论】:

  • 如果你在 Erlang shell 中运行 mnesia:table_info(my_auto_inc, type). 会得到什么?
  • 6> mnesia:table_info(my_auto_inc, type)。设置
  • 嗯,这只是一个想法,因为update_counter doesn't work on ordered_set tables,但似乎还可以......
  • 只是我希望有人与我验证函数 test()

标签: erlang


【解决方案1】:

您已使用记录人员创建了 person_backup 表,并且在写入时您正在使用导致错误的 person_backup 记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2014-04-10
    相关资源
    最近更新 更多