Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

mnesia - call an undefined function in erlang

I want to export DATA fom table mnesia to the txt file

I try with this code :

exporttxt()->
     F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
{atomic,L} = mnesia:transaction(F(user)),
file:write_file("test.txt",[io_lib:format("~p	~p	~p~n",[F1,F2,F3]) || 
                 #user{id = F1,adress = F2,birthday = F3} <- L]).

but when I test this function I have this error :

Erlang R13B03 (erts-5.7.4) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1> model:exporttxt().
** exception error: undefined function mensia:foldl/3
     in function  model:exporttxt/0
2> 

as you see I work with erlang version 13

I try now with this code :

exporttxt()->
     F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
{atomic,L} = mnesia:transaction(F(user)),
file:write_file("test.txt",[io_lib:format("~p	~p	~p~n",[F1,F2,F3]) || 
                 #user{id = F1,adress = F2,birthday = F3} <- L]).

but I have this error :

** exception exit: {aborted,no_transaction}
     in function  mnesia:abort/1
     in call from model:exporttxt/0

I try also with :

 exporttxt()->
         F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
    {atomic,L} = mnesia:transaction(F),
    file:write_file("test.txt",[io_lib:format("~p	~p	~p~n",[F1,F2,F3]) || 
                     #user{id = F1,adress = F2,birthday = F3} <- L]).

but I have this error :

** exception error: no match of right hand side value 
                    {aborted,{{badarity,{#Fun<model.208.16694406>,[]}},
                              [{mnesia_tm,apply_fun,3},
                               {mnesia_tm,execute_transaction,5},
                               {model,exporttxt,0},
                               {erl_eval,do_apply,5},
                               {shell,exprs,6},
                               {shell,eval_exprs,6},
                               {shell,eval_loop,3}]}}
     in function  model:exporttxt/0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
mnesia:transaction(F(user))

that is wrong, take a minute and look at it. mnesia:transaction/1 expect a function, you aren't passing it a function, you pass whatever the result of F(user) is.. (remember erlang uses strict evaluation, so it evaluates F(user) before mnesia:transaction/1).

In your case that call to F(user) fails because it was expected to run inside a transaction.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...