-5

我想将数据表 mnesia 导出到 txt 文件

我尝试使用此代码:

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\t~p\t~p~n",[F1,F2,F3]) || 
                 #user{id = F1,adress = F2,birthday = F3} <- L]).

但是当我测试这个功能时,我有这个错误:

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> 

如您所见,我使用 erlang 版本 13

我现在尝试使用此代码:

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\t~p\t~p~n",[F1,F2,F3]) || 
                 #user{id = F1,adress = F2,birthday = F3} <- L]).

但我有这个错误:

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

我也尝试:

 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\t~p\t~p~n",[F1,F2,F3]) || 
                     #user{id = F1,adress = F2,birthday = F3} <- L]).

但我有这个错误:

** 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
4

2 回答 2

2
mnesia:transaction(F(user))

这是错误的,请花点时间看看。mnesia:transaction/1 期望一个函数,你没有传递一个函数,你传递 F(user) 的任何结果.. (记住 erlang 使用严格的评估,所以它在mnesia:transaction/之前评估 F(user) 1)。

在您的情况下,对 F(user) 的调用失败,因为它应该在事务中运行。

于 2013-03-12T01:51:48.983 回答
1

这不是 Erlang 版本的问题。这是语句 mensia:foldl拼写错误的问题。它应该是 mnesia:foldl

于 2013-03-11T18:14:15.560 回答