更新erlang中的多个记录 [英] update multiple record in erlang

查看:261
本文介绍了更新erlang中的多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有此记录的表格人员

I have a table person with this record

-record(person, {id, firstname, lastname, phone}).

我想更新此表的所有记录的电话
用/ >

I want to update the phone of all records of this table Itry with

test()->
        Newphone ="216",
        Update=#person{phone=Newphone} ,
    Fun = fun() ->
                  List = mnesia:match_object(Update),
                  lists:foreach(fun(X) ->
                                        mnesia:write_object(X)
                                end, List)
          end,
    mnesia:transaction(Fun).

个人包含

 12  alen     dumas        97888888
    15  franco   mocci      55522225
    13  ali      othmani    44444449

我希望这个表格变成这样:

I want that this table became like this :

 12  alen     dumas      216
    15  franco   mocci      216
    13  ali      othmani    216

我尝试: / p>

I try with :

test()->
    Newphone ="216",
    Update=X#person{phone=Newphone, _ = '_'}
Fun = fun() ->
              List = mnesia:match_object(Update),
              lists:foreach(fun(X) ->
                                    mnesia:write(X)
                            end, List)
      end,
mnesia:transaction(Fun).

但是使用这段代码我有这个错误:

but with this code I have this error :

Variable X is unbound

这与此相关行:

Update=X#person{phone=Newphone, _ = '_'}

解决这个问题我做:

test()->
    Newphone ="216",
    Update=#person{phone=Newphone, _ = '_'}
Fun = fun() ->
              List = mnesia:match_object(Update),
              lists:foreach(fun(X) ->
                                    mnesia:write(X)
                            end, List)
      end,
mnesia:transaction(Fun).

当我测试我有这个消息:

when I test I have this message :

{atomic,ok}

但是当我咨询数据库我发现记录不更改

but when I consult the database I find that the records are not changed

我的代码中的难点是更改表的所有记录。 strong>

the difficulty in my code is to change all records of the table person

所以更改 97888888 55522225 44444449

这个值应该成为 216

推荐答案

@legoscia已经开始了。你的代码有几个问题:

Continuing from what @legoscia has started. There are a couple of problems left with your code:


  1. mnesia:match_object / 1 调用更新被用作模式,所以当你设置手机 phone = NewPhone 更新您实际上是在 match_object 电话价值216的所有记录。这不是你想要的。

  2. 您正在回写完全相同的记录。你不会在写回来之前更改记录。

  1. In the mnesia:match_object/1 call Update is being used as a pattern so when you set the phone field phone=NewPhone in Update you are actually saying to match_object give me all the records which have a phone of value "216". Which is not what you want.
  2. You are writing back exactly the same record as you matched. You are not changing the record before writing it back.

解决方案可能是(未经测试):

A solution could be (untested):

test()->
    Newphone ="216",
    Match=#person{_ = '_'},                               %Will match all records
    Fun = fun() ->
              List = mnesia:match_object(Match),
              lists:foreach(fun(X) ->
                                %% Create new record with phone=NewPhone and write it back
                                Update = X#person{phone=NewPhone},
                                mnesia:write(Update)
                            end, List)
          end,
    mnesia:transaction(Fun).

您在匹配中设置的任何字段将限制您将在 match_object 中匹配哪些记录。例如 Match = #person {phone =123,_ ='_'} 将匹配所有具有电话的记录123

Any fields you set in Match will limit which records you will match in match_object. For example Match = #person{phone="123",_='_'} will match all records which have phone "123".

这篇关于更新erlang中的多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆