我有表用户
-record(person, {id, firstname, lastname}).
此表包含以下值:
1 francoi mocci
2 test tes
我想从这个表中清除数据
我没有在erlang中找到从表用户中清除数据的语法
我找到了删除表的功能
mnesia:delete(..)
http://www.erlang.org/doc/man/mnesia.html#clear_table-1
clear_table(选项卡)
删除表选项卡中的所有条目。
要仅对某些记录执行此操作,您可以找到表中的所有键,然后为要删除的每个键调用删除函数。我在shell中尝试这个:
1> mnesia:create_schema([node()]).
ok
2> application:set_env(mnesia, dir, ".").
ok
3> application:start(mnesia).
ok
4> application:which_applications().
[{mnesia,"MNESIA CXC 138 12","4.7.1"},
{stdlib,"ERTS CXC 138 10","1.18.2"},
{kernel,"ERTS CXC 138 10","2.15.2"}]
5> rd(person, {id, firstname, lastname}).
person
6> mnesia:create_table(person,[{attributes, record_info(fields,person)},{ram_copies,[node()]}]).
{atomic,ok}
7> Add=fun(I,N,L) -> F = fun() -> mnesia:write(#person{id=I,firstname=N,lastname=L}) end, mnesia:activity(transaction,F) end.
#Fun<erl_eval.18.82930912>
8> Add(1,"Toto","Ralf").
ok
9> Add(2,"Titi","Ben").
ok
10> mnesia:activity(transaction,fun() -> mnesia:all_keys(person) end).
[1,2]
11> Add(3,"Tutu","Joe").
ok
12> L = mnesia:activity(transaction,fun() -> mnesia:all_keys(person) end).
[1,2,3]
13> mnesia:dirty_read(person,3).
[#person{id = 3,firstname = "Tutu",lastname = "Joe"}]
14> Del=fun(X) -> F=fun() -> mnesia:delete({person,X}) end, mnesia:activity(transaction,F) end.
#Fun<erl_eval.6.82930912>
15> [Del(X) || X <- L, X < 3].
[ok,ok]
16> mnesia:activity(transaction,fun() -> mnesia:all_keys(person) end).
[3]
17>