问题
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:
- In the
mnesia:match_object/1callUpdateis being used as a pattern so when you set thephonefieldphone=NewPhoneinUpdateyou are actually saying tomatch_objectgive me all the records which have aphoneof value"216". Which is not what you want. - 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:
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, _ = '_'}You probably don't want
Newphonein there—the record that you pass tomatch_objectshould 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.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 inX.The function
mnesia:write_objectdoesn't exist; you probably meant mnesia:write.
来源:https://stackoverflow.com/questions/14983345/update-multiple-record-in-erlang