_didem
How to display mnesia records with corresponding field names?
Hi,
I am new to Erlang. I am writing escript which makes database related operations. I list the records by using mnesia:foldl method as follows;
select_all(RemoteNode, TableName) ->
%% enumerate the records in a specific table
rpc:call( RemoteNode, mnesia, transaction, [
fun() -> mnesia:foldl(
fun(Rec,_Acc) -> io:format("~p\n",[Rec]) end,
[],
TableName)
end
]).
But this method returns list in tuple format. I want to listen records with corresponding field names.
After exploring the approach, I only find the following ;
-module('foo').
-author('Mats Cronqvist').
-export([go/0]).
-define(rec_info(T,R),lists:zip(record_info(fields,T),tl(tuple_to_list(R)))).
-record(r,{bla,foo,baz,bar}).
go() -> ?rec_info(r,#r{}).`
> Result -> foo:go().
> [{bla,undefined},
> {foo,undefined},
> {baz,undefined},
> {bar,undefined}]
I couldn’t understand the syntax of the method stated above. So I couldn’t manage to apply it to my script.
How can I display the records with field names ?
Thanks in advance,
Most Liked
tty
You can find the attributes (aka fields) of a table using record_info(fields, TableOrRecordName) which returns a list.
When you read from mnesia you get a tuple representation of the record. Hence the tuple_to_list(R).
However this representation includes the name of the record as the first element, we call this a named tuple. tl(tuple_to_list(R)) drops this name leaving you with just the attributes of the table.
In your specific case
fun() -> mnesia:foldl(
fun(Rec, _Acc) -> io:format(
"~p\n",
[lists:zip(record_info(fields, TableName, tl(tuple_to_list(Rec)) ))]
) end,
[], TableName)
end
cmkarlsson
If I understand you correctly you want to define an erlang record at runtime? This is not possible as erlang records are simply a compile time construct. Underneath they are in fact just tuples of the form {name, field1, field2, field3}.
There is no way to create or modify an erlang record outside of compile time.
This is not to say that you cannot work with the data from mnesia tables without records, but you will not get the record syntax to work with them. For example you could read the result of an mnesia read into a map or list of {name, value} tuples and access it this way as described earlier in this thread.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









