我有存儲在Excel文件將數據傳輸到二郎的Mnesia
該文件包含兩列數據:姓名
,幷包含幾行
我的目標是將這些數據註冊到我的數據庫mnesia(在包含兩個屬性名稱和姓氏的人員表中)
是否可以將數據從excel文件保存到mnesia數據庫
如果無法將數據從excel文件導入mnesia,我們可以將源文件(excel文件)轉換爲.sql或.txt,之後將該文件導入mnesia
我有存儲在Excel文件將數據傳輸到二郎的Mnesia
該文件包含兩列數據:姓名
,幷包含幾行
我的目標是將這些數據註冊到我的數據庫mnesia(在包含兩個屬性名稱和姓氏的人員表中)
是否可以將數據從excel文件保存到mnesia數據庫
如果無法將數據從excel文件導入mnesia,我們可以將源文件(excel文件)轉換爲.sql或.txt,之後將該文件導入mnesia
最簡單的方法,我認爲,是將excel數據導出/保存到csv文件,然後用像這樣的代碼從erlang解析它。
{ok, Data} = file:read_file("test.csv"),
ParsedData = lists:map(
fun(Str) -> string:tokens(Str, ",") end,
string:tokens(binary_to_list(Data), "\n")
),
lists:foreach(fun([K,V]) -> mnesia:write(K, V) end, ParsedData).
您需要將Excel文件轉換爲.csv
文件。那麼你可以使用我提供的CSV Parser
這裏:https://stackoverflow.com/a/10810660/431620。從Excel導入數據非常簡單直接。
編輯
我創建這個文件csv.erl:
%%% --- 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.
clean(Text,Char)->
string:strip(string:strip(Text,right,Char),left,Char).
,我在爲了從csv文件數據創建一個功能:
test()->
ForEachLine = fun(Line,Buffer)-> io:format("Line: ~p~n",[Line]),Buffer end,
InitialBuffer = [],
csv:parse("/home/include/person.csv",ForEachLine,InitialBuffer).
,並在二郎山的控制檯它displys正確的結果:
1> operation:test().
Line: ["50;97919861;5ab190537df;9898abb6e;efb65a0ad1;1",
"35427;1",
"3542;30-11-2012;testU2;testU6;undefined;m.;enabled;jjj;A999997;97979797;CIN;[email protected];b1088b51297846e;0;user;0;{{2012",
"11","30}","{1","59",
"26}};gggg;kkkk;ssss;standard;{2012","11",
"30};hhhh;mmm;undefined;18;yyyy;nnnn;false;{{2012",
"11","30}","{1","59","26}};1989;1;1"]
ok
現在我的目標是寄存器在Mnesia數據庫這個數據(表中的人)
表人已49屬性
在具有這種結構的表格人所以寄存器這一行
Line: ["50;97919861;5ab190537df;9898abb6e;efb65a0ad1;1",
"35427;1",
"3542;30-11-2012;testU2;testU6;undefined;m.;enabled;jjj;A999997;97979797;CIN;[email protected];b1088b51297846e;0;user;0;{{2012",
"11","30}","{1","59",
"26}};gggg;kkkk;ssss;standard;{2012","11",
"30};hhhh;mmm;undefined;18;yyyy;nnnn;false;{{2012",
"11","30}","{1","59","26}};1989;1;1"]
:
-record(person, {id, token, password, pin, key, salt, pin_salt, subscription_date, first_name, last_name, alias, gender, status,
taxid, formid, idcard, id_type, email, activation_code, email_confirmation, role,
trying, last_access, last_activity, last_activity_ip, last_activity_op_code, group, last_mod, last_login,
agent_code, agency_code, parent_id,pass_changes, pin_changes, is_merchant, created_at, birth_year, birth_month, birth_date}).