如何在erlang中读取mnesia数据库的所有记录? [英] How to read all the records of mnesia database in erlang?

查看:335
本文介绍了如何在erlang中读取mnesia数据库的所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是erlang的新手,我需要对从mnesia数据库获得的所有记录进行一些操作。

I ma new in erlang and I need to do some operations for all records I get from mnesia database.

Result = mnesia:dirty_read(mydatabase, {key1, key2}),
        case Result of 
            [] ->
                ?DEBUG("No such record found", []);
            [#mydatabase{key3 = Key3}] ->
                %% some operations
        end

如何向我的循环添加对所有记录执行某些操作的代码?

How can I add a loop to my code that execute some operations for all records?

我什至不确定上面的代码是否执行?

I am not even sure if the code above does it or not?

推荐答案

您可以使用 mnesia:foldl / 3 。遍历表中的所有记录,并传递累加器值。

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

它没有明确的脏副本,因此如果要运行作为肮脏的操作,您需要使用 mnesia:activity / 2 。 (或者,您也可以在对 mnesia的调用中使用它:交易 。)

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.)

在此示例中,我实际上对累加器不做任何事情,而保留为 ignored_acc

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)

这篇关于如何在erlang中读取mnesia数据库的所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆