从mnesia导出数据到excel [英] export data from mnesia to excel

查看:435
本文介绍了从mnesia导出数据到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有表用户

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

此表包含以下值:

  1 francoi mocci 
2 test tes

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



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



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

  %%% --- csv解析器在Erlang。 ------ 
%%%帮助处理大型csv文件,而无需将它们加载到
%%%内存中。类似于SAX

-module(csv)的xml解析技术。
-compile(export_all)。

parse(FilePath,ForEachLine,Opaque) - >
案例文件:open(FilePath,[read])
{_,S} - >
start_parsing(S,ForEachLine,不透明);
错误 - >错误
结束。

start_parsing(S,ForEachLine,不透明) - >
Line = io:get_line(S,''),
case行
eof - > 【OK,不透明};
\\\
- > start_parsing(S,ForEachLine,不透明);
\r\\\
- > start_parsing(S,ForEachLine,不透明);
_ - >
NewOpaque = ForEachLine(scan(clean(clean(Line,10),13)),Opaque),
start_parsing(S,ForEachLine,NewOpaque)
end。

scan(InitString,Char,[Head | Buffer])当Head == Char - >
{lists:reverse(InitString),Buffer};
scan(InitString,Char,[Head | Buffer])当Head = / = Char - >
扫描([Head | InitString],Char,Buffer);
scan(X,_,Buffer)当Buffer == [] - > {完成后,列表:反向(X)}。
扫描仪(文本) - >列表:反向(traverse_text(文字,[]))。

%% traverse_text(Text,Buff) - >
%% case scan(,$ ,, Text)
%% {done,SomeText} - > [SomeText |巴夫];
%% {Value,Rem} - > traverse_text(Rem,[Value | Buff])
%% end。


traverse_text(Text,Buff) - >
案例扫描(,$;文本)
{done,SomeText} - > [SomeText |巴夫];
{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,名字,姓氏] =行,

%%这里插入每行到表mnesia

缓冲区结束,


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

但我有这个错误:

 之前的语法错误:'。'

此错误与这行:

  #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。

但我现在有这个错误:

 变量'F'是未绑定的

此错误与这行:

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

我解决了这个问题:

 试验() - > 
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])。

但是当我运行我的功能我有这个错误:

  **异常错误:右侧不匹配值{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}]}}
在函数模型中: test / 0

我尝试使用这段代码:

 试验() - > 
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])。

但我也有这个错误:

  **异常错误:右侧不匹配值{aborted,{undef,[{mensia,foldl,
[#Fun< model.21.662230>,[] ,
{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}]}}
在功能模型中:test / 0


解决方案

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



编辑: / strong>对不起,我没有测试行...现在应该可以工作。

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





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};
        "\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 :

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

这篇关于从mnesia导出数据到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆