我在mnesia中有两个具有以下格式的表:
mnesia:create_table(person,
[{disc_copies, [node()]},
{attributes, record_info(fields, person)}]),
mnesia:create_table(person_backup,
[{disc_copies, [node()]},
{attributes, record_info(fields, person)},
{record_name, person}]),
我想开发一个具有这个角色的函数:
从表格中读取所有数据 然后在表person_backup
中写下这些数据我尝试使用备份功能(这是你的代码)
testbackup()->
mnesia:transaction(fun() ->
Records = mnesia:select(person, [{'_', [], ['$_']}]),
[ok = mnesia:write(person_backup, Record, write) || Record <- Records]
end).
当我运行此功能时,我收到此消息
model:testbackup().
{atomic,[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,
ok,ok,ok,ok,ok,ok,ok,ok,ok,ok|...]}
它在表中完美地表示person_backup我有相同的表人数据
但是当我做模特时:reset()。
表人的数据和表person_backup将被删除
通常juste将删除人的数据
reset()的代码是:
reset() ->
stop(),
destroy(),
create(),
start(),
{ok}.
destroy() ->
mnesia:start(),
mnesia:delete_table(counter),
mnesia:delete_table(person),
mnesia:stop(),
mnesia:delete_schema([node()]).
create() ->
mnesia:create_schema([node()]),
mnesia:start(),
mnesia:create_table(counter, [{attributes, record_info(fields, counter)}, {disc_copies, [node()]}]),
mnesia:create_table(person, [{attributes, record_info(fields, person)}, {disc_copies, [node()]}]),
mnesia:create_table(person_backup,[{disc_copies, [node()]},{attributes, record_info(fields, person)},
{record_name, person}]),
mnesia:stop().
我试图以另一种方式解决这个问题
我有一个功能 testcreate
testcreate()->
%% mnesia:create_schema([node()]),
mnesia:start(),
mnesia:create_table(person_backup, [{attributes, record_info(fields, person_backup)}, {disc_copies, [node()]}]).
并且在记录中我有
-record(person, {id, token, password, pin, key, salt, pin_salt, subscription_date, first_name, last_name, alias, gender, status,
taxid, formid, idcard, birth_year, birth_month, birth_date}).
-record(person_backup, {id, token, password, pin, key, salt, pin_salt, subscription_date, first_name, last_name, alias, gender, status,
taxid, formid, idcard, birth_year, birth_month, birth_date}).
当我运行 testbackup 功能时,我收到了此消息
2> model:testbackup().
{aborted,{bad_type,{person,215,"97808233",
"bddcba13effb029e93aaab6fdc3c4587",
"d707aa5f940a468e149686b3eaafd946",
"230d8294d47f6fa2cc1761deab52a879",
"1360713353326653","1360713353326653",undefined,
"souad","sallami",[],"M.","preregistered",
undefined,"Z008022","04705808","CIN",[],
"d41d8cd98f00b204e9800998ecf8427e","0","user",0,
{{...},...},
undefined,...}}}
答案 0 :(得分:0)
这样的事情:
mnesia:transaction(fun() ->
Records = mnesia:select(person, [{'_', [], ['$_']}]),
[ok = mnesia:write(person_backup, Record, write) || Record <- Records]
end).