我在 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 中读取所有数据,然后将这些数据写入表 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 表中我有与表 person 相同的数据
但是当我做模型时:reset()。
表 person 和 table person_backup 的数据将被删除
并且通常会删除人的数据
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,...}}}