(1)mnesia数据写入文件
write_db_to_file()->
Tables = [Table || Table <- mnesia:system_info(tables), string:str(atom_to_list(Table), atom_to_list(?MODULE)) =/= 0],
Fun =
fun(PlayerTable)->
MatchHead = #loot_gold_db{ _ = '_'},
Guard = [],
Result = ['$_'],
mnesia:select(PlayerTable, [{MatchHead, Guard, Result}])
end,
DataList =
lists:foldl(fun(Table, Acc)->
case mnesia:transaction(Fun, [Table]) of
{aborted, _Reason} ->
Acc;
{atomic, []}->
Acc;
{atomic, LootGoldDBList}->
LootGoldDBList ++ Acc
end
end, [], Tables),
filelib:ensure_dir("./temp_db/"),
case file:open("./temp_db/loot_gold_db" , [write]) of
{ok, IoDevice}->
lists:foreach(fun(Data)-> io:format(IoDevice, "~p.~n", [Data]) end, DataList),
file:close(IoDevice);
_->
io:format("==================write_db_to_file_is_error==================~n")
end.
(2)把文件数据写入mnesia
case file:consult("./temp_db/loot_gold_db") of
{ok, DataList}->
lists:foreach(fun(Data)-> save_to_db(Data) end, DataList);
_->
io:format("==================write_file_to_db_is_error==================~n")
end.