我有这个表:
-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
表 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}}
我也尝试使用此代码:
测试2()->
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}]}}