-1

我有表用户

  -record(person, {id, firstname, lastname}).

此表包含以下值:

    1  francoi     mocci     
    2  test        tes  

我的目标是如何将这些数据从 mnesia 导出到 excel

我知道相反的方式意味着将数据从 excel 传输到 mnesia

这种情况下的解决方案是在 csv.file 中转换 excel,然后使用这种代码来解析 csv 文件:

%%% --- csv parser in Erlang. ------
%%% To help process large csv files without loading them into
%%% memory. Similar to the xml parsing technique of SAX

-module(csv).
-compile(export_all).

parse(FilePath,ForEachLine,Opaque)->
    case file:open(FilePath,[read]) of
        {_,S} ->
            start_parsing(S,ForEachLine,Opaque);
        Error -> Error
    end.

start_parsing(S,ForEachLine,Opaque)->
    Line = io:get_line(S,''),
    case Line of
        eof -> {ok,Opaque};
        "\n" -> start_parsing(S,ForEachLine,Opaque);
        "\r\n" -> start_parsing(S,ForEachLine,Opaque);
        _ -> 
            NewOpaque = ForEachLine(scanner(clean(clean(Line,10),13)),Opaque),
            start_parsing(S,ForEachLine,NewOpaque)
    end.

scan(InitString,Char,[Head|Buffer]) when Head == Char -> 
    {lists:reverse(InitString),Buffer};
scan(InitString,Char,[Head|Buffer]) when Head =/= Char ->
    scan([Head|InitString],Char,Buffer);
scan(X,_,Buffer) when Buffer == [] -> {done,lists:reverse(X)}.
scanner(Text)-> lists:reverse(traverse_text(Text,[])).

%%traverse_text(Text,Buff)->
  %%  case scan("",$,,Text) of
    %%    {done,SomeText}-> [SomeText|Buff];
 %%       {Value,Rem}-> traverse_text(Rem,[Value|Buff])
   %% end.


traverse_text(Text,Buff)->
    case scan("",$;,Text) of
        {done,SomeText}-> [SomeText|Buff];
        {Value,Rem}-> traverse_text(Rem,[Value|Buff])
    end.


clean(Text,Char)-> 
    string:strip(string:strip(Text,right,Char),left,Char).

这是将数据从 csv 文件插入到 mnesia 的函数示例:

test()->

    ForEachLine = fun(Line,Buffer)->
   [Id, Firstname, Lastname] = Line,

                                    %% here insert each line to the table mnesia

                                     Buffer end,


 InitialBuffer = [],




 csv:parse("/home/test/Desktop/testt.csv",ForEachLine,InitialBuffer).

这个例子没有问题

我尝试使用此代码:

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

但我有这个错误:

syntax error before : '.' 

此错误与此行有关:

#person{id = F1,firstname = F2,lastname = F3} <- L]).

我尝试使用以下方法更正我的代码:

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

但我现在有这个错误:

variable 'F' is unbound 

此错误与此行有关:

{atomic,L} = mnesia:transaction(F),

我解决了这个问题:

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

但是当我运行我的函数时,我有这个错误:

** exception error: no match of right hand side value {aborted,{{badarity,{#Fun<model.20.69991685>,[]}},
                                                                [{mnesia_tm,apply_fun,3},
                                                                 {mnesia_tm,execute_transaction,5},
                                                                 {model,test,0},
                                                                 {erl_eval,do_apply,5},
                                                                 {shell,exprs,6},
                                                                 {shell,eval_exprs,6},
                                                                 {shell,eval_loop,3}]}}
     in function  model:test/0

我尝试使用此代码:

test()->
      F = fun() -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],person)end,
{atomic,L} = mnesia:transaction(F),
file:write_file("filename.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) || 
                 #person{id = F1,firstname = F2,lastname = F3} <- L]).

但我也有这个错误:

** exception error: no match of right hand side value {aborted,{undef,[{mensia,foldl,
                                                                               [#Fun<model.21.662230>,[],person]},
                                                                       {mnesia_tm,apply_fun,3},
                                                                       {mnesia_tm,execute_transaction,5},
                                                                       {model,test,0},
                                                                       {erl_eval,do_apply,5},
                                                                       {shell,exprs,6},
                                                                       {shell,eval_exprs,6},
                                                                       {shell,eval_loop,3}]}}
     in function  model:test/0
4

2 回答 2

1

您忘记end了第一行:

F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
于 2013-03-11T13:56:46.113 回答
1

您可以使用 foldl 函数创建一个列表,然后将此列表写入文件,使用任何字符作为分隔符(空格、逗号、制表符...取决于记录的内容),最后使用 Excel 读取文本文件,您将有一个弹出菜单,可帮助您控制 excel 解释数据的方式。我认为最好使用中间列表,因为直接写入文件对于数据库事务可能很长。

编辑:对不起,我没有测试这条线......它现在应该可以工作了。

...
F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
{atomic,L} = mnesia:transaction(F(mnesia_table)),
file:write_file("filename.txt",[io_lib:format("~p\t~p~n",[F1,F2]) || 
                 #table_record{field1 = F1,field2 = F2} <- L]),
...

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

于 2013-03-11T12:47:56.403 回答