Mnesia数据库增加与查询学习
-module(test_mnesia).
-record(shop, {item, quantity, cost}).
-record(cost, {name, price}).
-record(design, {info, plan}).
%% API
-export([insert/3,select/1,start/0]).
start() ->
mnesia:start().
insert(Name, Quantity, Cost) ->
Row = #shop{item = Name, quantity = Quantity, cost = Cost},
F = fun() ->
mnesia:write(Row)
end,
mnesia:transaction(F).
select() ->
qlc:q([X || X <- mnesia:table(shop)]).
能够插入但是不能查询
需要对do函数进行实现,才能展示出数据库中的数据,do函数在书上20.4.3有相应实现
-module(test_mnesia).
-include_lib("stdlib/include/qlc.hrl").
-record(shop, {item, quantity, cost}).
%% API
-export([insert/3,select/0,start/0,do_this_once/0]).
start() ->
mnesia:start().
insert(Name, Quantity, Cost) ->
Row = #shop{item = Name, quantity = Quantity, cost = Cost},
io:format("inserting ~p~n", [Row]),
F = fun() ->
mnesia:write(Row)
end,
mnesia:transaction(F).
select() ->
do(qlc:q([X || X <- mnesia:table(shop)])).
do(Q) ->
F = fun
() ->
qlc:e(Q)
end,
{atomic,Val} = mnesia:transaction(F),
Val.
实现do函数后的代码运行截图