update multiple record in erlang

你说的曾经没有我的故事 提交于 2019-12-16 18:07:08

问题


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

The table person contains

 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

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

this is related to this line :

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

to resolve this probleme I do :

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

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

so change 97888888 and 55522225 and 44444449

this values should became 216


回答1:


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

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

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".




回答2:


A few things need changing here:

  1. If you are going to use a record as a template for mnesia:match_object, you should fill in the record fields that you don't care about with the atom '_'. There is a special syntax to do that:

    Update=#person{phone=Newphone, _ = '_'}
    
  2. You probably don't want Newphone in there—the record that you pass to match_object should match the objects that are already in the table but should be changed, not what you want the objects to be changed to.

    Which records are you trying to change? That will determine what you should pass to match_object.

  3. The only thing you do in the transaction is reading records and writing them back unchanged. Have a look at the Records chapter of the Erlang reference manual; you probably want something like X#person{phone = Newphone} to change the phone field in X.

  4. The function mnesia:write_object doesn't exist; you probably meant mnesia:write.



来源:https://stackoverflow.com/questions/14983345/update-multiple-record-in-erlang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!