【问题标题】:function which contains read and write from two table in erlang包含从erlang中的两个表读取和写入的函数
【发布时间】:2013-02-18 23:27:11
【问题描述】:

我在 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表和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,...}}}

【问题讨论】:

    标签: erlang


    【解决方案1】:

    类似这样的:

    mnesia:transaction(fun() ->
      Records = mnesia:select(person, [{'_', [], ['$_']}]),
      [ok = mnesia:write(person_backup, Record, write) || Record <- Records]
    end).
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2016-08-19
      • 2019-11-15
      • 2019-06-05
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      相关资源
      最近更新 更多