Can't fill in the mnesia table

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can't fill in the mnesia table

Abhishek Ranjan-2

I wrote this code snippet:

Player=t:user(List),
        CatchAll=[{'_',[],['$_']}],
        NameList=mnesia:dirty_select(player, CatchAll),
          if
              (length(NameList)<4)->
                  T3 = fun() ->
                  X3 = #player{name=Player
                                },
                      mnesia:write(X3)
                      end,
                      mnesia:transaction(T3),
                  NameList1=mnesia:dirty_select(player, CatchAll),
                  ?io:format("1The value of NameList1 is ~p~n",[NameList1]),
                  ok;
              (length(NameList)==4)->
                  ?io:format("2The value of NameList is ~p~n",[NameList])
          end,       

Here, each time a call a module t:user I get a name which i have stored in Player and want to keep storing these names till there are 4 of them but each i time I run this I get an empty value of Namelist as [], instead of a list filled with players name.

Any Idea on why this is happening?


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can't fill in the mnesia table

Dániel Szoboszlay
Hi,

Your code works for me (apart from the obvious race condition that you may end up with more than 4 records in the table if at least two processes execute the code concurrently), so my best guess is that the mnesia:transaction(T3) call fails in your database for some reason. Try capturing it's return value and adding it to the debug printout!

Cheers,
Daniel

On Mon, 10 Jul 2017 at 09:43 Abhishek Ranjan <[hidden email]> wrote:

I wrote this code snippet:

Player=t:user(List),
        CatchAll=[{'_',[],['$_']}],
        NameList=mnesia:dirty_select(player, CatchAll),
          if
              (length(NameList)<4)->
                  T3 = fun() ->
                  X3 = #player{name=Player
                                },
                      mnesia:write(X3)
                      end,
                      mnesia:transaction(T3),
                  NameList1=mnesia:dirty_select(player, CatchAll),
                  ?io:format("1The value of NameList1 is ~p~n",[NameList1]),
                  ok;
              (length(NameList)==4)->
                  ?io:format("2The value of NameList is ~p~n",[NameList])
          end,       

Here, each time a call a module t:user I get a name which i have stored in Player and want to keep storing these names till there are 4 of them but each i time I run this I get an empty value of Namelist as [], instead of a list filled with players name.

Any Idea on why this is happening?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Loading...