_didem

_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

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

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.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement