我有两个表计数器和计数
-record(counter, {key, value}).
-record(count, {key, value}).
create() ->
mnesia:create_schema([node()]),
mnesia:start(),
mnesia:create_table(counter, [{attributes, record_info(fields, counter)}, {disc_copies, [node()]}]),
mnesia:create_table(count, [{attributes, record_info(fields, count)}, {disc_copies, [node()]}]),
mnesia:stop().
但是当我尝试运行此功能时
backup()->
mnesia:transaction(fun() ->
Records = mnesia:select(counter, [{'_', [], ['$_']}]),
[ok = mnesia:write(count, Record, write) || Record <- Records]
end).
我有这个错误
2> model:backup().
{aborted,{bad_type,{counter,test,353}}}
当我使用你的代码时:
mnesia:create_table(count, [{record_name, counter}, {attributes, record_info(fields, counter)}, {disc_copies, [node()]}]).
当我尝试运行model:reset()时,它会删除表计数和表计数器的数据
我的目标是删除计数器
的数据这是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().
答案 0 :(得分:1)
您正在尝试在计数表中写入计数器记录,这会导致错误。有两种方法可以做到这一点。
使用一个名为counter的记录,并将count用作count表 mnesia:create_table(count,[ {record_name,counter} ,{attributes,record_info(fields,counter)},{disc_copies,[node()]}]。
如果您有其他字段,则无法进行上述操作,您可以在写入之前将所需的元素从计数器记录复制到计数记录。