0

我有两个表计数器和计数

   -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().
4

1 回答 1

1

您正在尝试在计数表中写入计数器记录,这会导致错误。有两种方法可以做到这一点。

  1. 要么使用一个名为 counter 的记录,也可以将 in 用于计数表 mnesia:create_table(count, [ {record_name, counter} , {attributes, record_info(fields, counter)}, {disc_copies, [node()]}]) .

  2. 如果如果您有其他字段则无法执行上述操作,您可以在写入之前将所需元素从计数器记录复制到计数记录。

于 2013-02-18T17:26:19.420 回答