[erlang-questions] How best to extract value from mnesia query

Robert Raschke rtrlists@REDACTED
Wed Nov 15 22:46:43 CET 2017


If I know that I shall only receive a single value result, I write

{atomic, [Value]} = mnesia:transaction(...)

This has the side effect of neatly terminating the process if we didn't get
exactly one value back as a result. And that allows me to catch the error
and decide what to do about it.

If you want to write defensively, then something that discriminates between
the results may be appropriate.

case mnesia:transaction(...) of
    {aborted, Reason} -> ...;
    {atomic, []} -> ...;
    {atomic, [Value]} -> ...;
    {atomic, Values} -> ...
end

Cheers,
Robby


On 15 Nov 2017 21:04, <lloyd@REDACTED> wrote:

Hello,

The result of a mnesia read transaction looks like this:

   {atomic, Results} where result is either a populated list [value1, ...
valueN] or, an empty list [].

If the table is initialized as set, then the returned list will contain at
most one value, e.g:

  [value].

If I want to use of the value in a function or to populate another record,
I need to extract the value from the list.

I've tried various ways to do this, but they all seem clumsy:

E.g.

[MyValue]

hd[vlaue]

confirm([]) ->
    not_found;
confirm([Value]) ->
    Value;
confirm(Value) ->
    Value.

Does there happen to be a preferred/conventional/best-practices way to do
this?

Many thanks,

LRP






_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171115/d2640a02/attachment.htm>


More information about the erlang-questions mailing list