在erlang清除表

时间:2013-02-27 17:28:15

标签: erlang mnesia

我有表用户

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

此表包含以下值:

    1  francoi     mocci     
    2  test        tes  

我想清除此表中的数据

我没有在erlang中找到语法来清除表用户的数据

我找到了用

删除表格的功能
mnesia:delete(..)

2 个答案:

答案 0 :(得分:7)

http://www.erlang.org/doc/man/mnesia.html#clear_table-1

  

clear_table(Tab)

     

删除表格中的所有条目。

答案 1 :(得分:2)

要仅对某些记录执行此操作,您可以找到表中的所有键,然后为要删除的每个键调用delete函数。我在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>