如何在erlang中读取mnesia数据库的所有记录? [英] How to read all the records of mnesia database in erlang?
问题描述
我是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屋!