[erlang-questions] How best to return db query

Bernard Duggan <>
Mon Feb 8 01:25:55 CET 2016


>An empty result to a db query is not an error.
That rather depends on the semantics of your program. In the general case,
you're absolutely right, of course - that's why mnesia:read() doesn't
return an error in that case. However, perhaps in the case of Lloyd's
application, there should always be something in that particular table -
maybe something populated at startup that nothing should ever delete. In
that case it would be perfectly reasonable to not just produce an error,
but in fact to crash.
So I guess what I'm saying is that my answer would be "it depends on what
you want the semantics of get_note to be". Is it expected to always return
one or more values? Is it an error if it doesn't, and if so, is that error
sensibly recoverable by the caller?

Cheers,

B

On Mon, Feb 8, 2016 at 5:45 AM, Sergej Jurečko <>
wrote:

> An empty result to a db query is not an error.
>
> Sergej
> On Feb 7, 2016 7:38 PM, <> wrote:
>
>> Hello,
>>
>> I have this mnesia query:
>>
>>
>> get_note(ID) ->
>>     Query =
>>         fun() ->
>>             mnesia:read({?TABLE, ID})
>>         end,
>>     {atomic, Results} = mnesia:transaction(Query),
>>     Results.
>>
>> It returns either a list containing one or more valid records or an empty
>> list.
>>
>> Would it be better to return something like:
>>
>> {ok, Records} | {error, data_not_found}
>>
>> Thanks,
>>
>> LRP
>>
>>
>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160208/f86927d0/attachment.html>


More information about the erlang-questions mailing list