_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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement