Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...