【问题标题】:clear table in erlang在erlang中清除表格
【发布时间】:2013-02-13 15:09:31
【问题描述】:

我有表用户

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

此表包含此值:

    1  francoi     mocci     
    2  test        tes  

我要清除该表中的数据

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

我找到了删除表的函数

mnesia:delete(..)

【问题讨论】:

    标签: erlang mnesia


    【解决方案1】:

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

    clear_table(标签)

    删除表格 Tab 中的所有条目。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 2018-03-28
        • 1970-01-01
        相关资源
        最近更新 更多