How to read all the records of mnesia database in erlang?

耗尽温柔 提交于 2019-12-05 09:53:15

You could use mnesia:foldl/3 for that. It iterates over all records in a table, passing along an "accumulator" value.

It doesn't have an explicit "dirty" counterpart, so if you want to run it as a dirty operation you need to use mnesia:activity/2. (Or you could just use it inside a call to mnesia:transaction.)

In this example, I don't actually do anything with the "accumulator", leaving as ignored_acc throughout.

mnesia:activity(sync_dirty,
  fun() ->
      mnesia:foldl(
          fun(#mydatabase{}, Acc) ->
              %% do something with the record here
              Acc
          end,
          ignored_acc,
          my_table)
  end)

I think you can try all_keys(Tab)

all_keys(Tab) -> KeyList | transaction abort

This function returns a list of all keys in the table named Tab. The semantics of this function is context sensitive. See mnesia:activity/4 for more information. In transaction context it acquires a read lock on the entire table.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!