mnesia

1、模式创建

mnesia:create_schema([node()|nodes()]).集群的节点之间创建模式

2、启动和停止

application:start(mnesia).

application:stop(mnesia).

3、创建表

rd(usr,{msisdn,id,status,plan,services}).                                                                        usr
(om@publicGroup1)18> Fields = record_info(fields,usr).
[msisdn,id,status,plan,services]
(om@publicGroup1)19> mnesia:create_table(usr,[{disc_copies,[node()]},{ram_copies,nodes()},{type,set},{attributes,Fields},{index,[id]}]).
{atomic,ok}

4、事务

transaction(F).

mnesia:transaction(fun() -> mnesia:index_read(usr, 700000002,id) end).

mnesia:transaction(fun() -> mnesia:write(usr, Usr#usr{services=[ok]}).

5、遍历

(foo@publicGroup1)41> Fun = fun(#muppet{name="Francesco",salary = Salary},_) ->  
(foo@publicGroup1)41> Salary;                                                    
(foo@publicGroup1)41> (_,_) -> ok                                                
(foo@publicGroup1)41> end.
#Fun<erl_eval.12.111823515>
(foo@publicGroup1)42> mnesia:transaction(fun() -> mnesia:foldl(Fun,ok,muppet) end).
{atomic,0}

6、脏操作

mnesia:dirty_index_read(usr,700000002,id).

mnesia:dirty_read({usr,PhoneNo}).

 

你可能感兴趣的:(a)