简体   繁体   English

将数据从mnesia导出到excel

[英]export data from mnesia to excel

I have the table user 我有桌子的用户

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

this table contains this values : 该表包含以下值:

    1  francoi     mocci     
    2  test        tes  

my goal is how can I export this data from mnesia to excel 我的目标是如何将数据从mnesia导出到excel

I know the inverse way meaning transfer data from excel to mnesia 我知道相反的方式,即从Excel到Mnesia传输数据

the solution in this case is to converse the excel in csv.file then use this kind of code to parse the csv file : 在这种情况下,解决方案是在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).

and this is an example of function to insert data from csv file to mnesia : 这是将数据从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).

and this example had no problem 这个例子没有问题

I try with this code : 我尝试使用以下代码:

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]).

but I have this error : 但我有这个错误:

syntax error before : '.' 

this error is related to this line : 此错误与此行有关:

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

I try to correct my code with : 我尝试使用以下方式更正我的代码:

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.

but I have now this error : 但是我现在有这个错误:

variable 'F' is unbound 

this error is related to this line : 此错误与此行有关:

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

I solved this problem with : 我用以下方法解决了这个问题:

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]).

but when I run my function I have this error : 但是当我运行我的函数时,我遇到了这个错误:

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

I try with this code : 我尝试使用以下代码:

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]).

but I also have this error : 但我也有这个错误:

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

You can use the foldl function to create a list and then write this list to a file, using any character as separator (space,comma,tab... depending on the content of your records) and last read the text file with Excel, you will have a popup menu that help you to control the way excel interpret the data. 您可以使用foldl函数创建列表,然后将此列表使用任何字符作为分隔符(空格,逗号,制表符...,具体取决于记录的内容)写入文件,最后使用Excel读取文本文件,您将有一个弹出菜单,可帮助您控制excel解释数据的方式。 I think it is better tu use an intermediate list, because writing to a file directly may be long for a database transaction. 我认为最好使用中间列表,因为直接写入文件对于数据库事务可能很长。

EDIT: Sorry, I didn't test the line... it should work now. 编辑:对不起,我没有测试这条线...它现在应该可以工作。

...
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]),
...

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

您忘了在第一行end

F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM