Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

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

I know the inverse way meaning transfer data from excel to 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 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};
        "
" -> start_parsing(S,ForEachLine,Opaque);
        "
" -> 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 :

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	~p	~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	~p	~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	~p	~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	~p	~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
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You forgot end in the first line:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...