博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[erlang] mnesia
阅读量:6267 次
发布时间:2019-06-22

本文共 4108 字,大约阅读时间需要 13 分钟。

原文地址: http://www.cnblogs.com/bluefrog/archive/2012/05/16/2504625.html

本来是项目合作的,可是你却一而再再而三的使用这招,我处理愤怒了,自己来。先学习下 mnesia 的操作先:

-module(test_mnesia).-compile(export_all).-include_lib("stdlib/include/qlc.hrl").%% 定义记录结构-record(shop,{item,quantity,cost}).-record(cost,{name,price}).-record(design,{id,plan}).start() ->    mnesia:start(),    %% 等待表的加载    mnesia:wait_for_tables([shop,cost,design],20000).%% 初始化mnesia表结构init() ->    mnesia:create_schema([node()]),    mnesia:start(),    %% 表创建 mnesia:create_talbe(TableName,[Args])    %% {type,Type}  set,ordered_set,bag 表类型    %% {ram_copies,NodeList} NodeList每个节点都有内存备份 默认为这个{ram_copies,[node()]}    %% {disc_copies,NodeList} NodeList每个节点都有内存备份和磁盘备份     %% {disc_only_copies,NodeList} NodeList每个节点有磁盘备份     %% {attributes,AtomList} 要保存的列名称 一般和record有关 record_info(fields,RecordName)    mnesia:create_table(shop,[{attributes,record_info(fields,shop)}]), %% 创建shop表    mnesia:create_table(cost,[{attributes,record_info(fields,cost)}]),    mnesia:create_table(design,[{attributes,record_info(fields,design)}]),    mnesia:stop().%% 加载测试数据reset_tables() ->    mnesia:clear_table(shop),    mnesia:clear_table(cost),    F = fun() ->            lists:foreach(fun mnesia:write/1,example_tables())    end,    mnesia:transaction(F).%% 测试数据example_tables() ->    [        %% shop table        {shop,apple,20,2.3},        {shop,orange,100,3.8},        {shop,pear,200,3.6},        {shop,banana,420,4.5},        {shop,potato,2456,1.2},        %% cost table        {cost,apple,1.5},        {cost,orange,2.4},        {cost,pear,2.2},        {cost,banana,1.6},        {cost,potato,0.6}    ].%%== 查询 =============================================================do(Q) ->    F = fun() -> qlc:e(Q) end,    {atomic,Val} = mnesia:transaction(F),    Val.%% SELECT * FROM shop%% 选取所有列demo(select_shop) ->    do(qlc:q([X || X <- mnesia:table(shop)]));%% SELECT  item,quantity FROM shop%% 选取指定列demo(select_some) ->    do(qlc:q([{X#shop.item, X#shop.quantity} || X <- mnesia:table(shop)]));%% SELECT * FROM shop WHERE shop.quantity < 250%% 选取指定条件的数据demo(where) ->    do(qlc:q([X || X <- mnesia:table(shop),                X#shop.quantity < 250            ]));%% 关联查询%% SELECT shop.* FROM shop,cost wHERE shop.item = cost.name AND cost.price < 2 AND shop.quantity < 250demo(join) ->    do(qlc:q([X || X <- mnesia:table(shop),                   X#shop.quantity < 250,                   Y <- mnesia:table(cost),                   X#shop.item =:= Y#cost.name,                   Y#cost.price < 2            ])).%% == 数据操作 ===============================================%% 增加一行add_shop_item(Name,Quantity,Cost) ->    Row = #shop{item = Name,quantity = Quantity, cost = Cost},    F = fun() ->            mnesia:write(Row)    end,    mnesia:transaction(F).%% 删除一行remove_shop_item(Item) ->    Oid = {shop,Item},    F = fun() ->            mnesia:delete(Oid)    end,    mnesia:transaction(F).%% 取消一个事务former(Nwant) ->    F = fun() ->            %% find the num of apples            [Apple] = mnesia:read({shop,apple}),            Napples = Apple#shop.quantity,            %% update the database            NewApple = Apple#shop{quantity = Napples + 2 * Nwant},            mnesia:write(NewApple),            %% find the num of oranges            [Orange] = mnesia:read({shop,orange}),            Noranges = Orange#shop.quantity,            if                 Noranges >= Nwant ->                    %% update the database                    Num = Noranges - Nwant,                    NewOrange = Orange#shop{quantity = Num},                    mnesia:write(NewOrange);                true ->                    %% no enough oranges 取消事务                    mnesia:abort(oranges)            end    end,    mnesia:transaction(F).%% 保存复杂数据add_plans() ->    D1 = #design{        id = {joe,1},        plan = {circle,10}    },    D2 = #design{        id = fred,        plan = {rectangle,[10,5]}    },    F = fun() ->            mnesia:write(D1),            mnesia:write(D2)    end,    mnesia:transaction(F).%% 获复杂数据get_plans(PlanId) ->    F = fun() -> mnesia:read({design,PlanId}) end,    mnesia:transaction(F).

 

你可能感兴趣的文章
App 卸载记录
查看>>
南京大学周志华教授当选欧洲科学院外籍院士
查看>>
计算机网络与Internet应用
查看>>
Django 文件下载功能
查看>>
走红日本 阿里云如何能够赢得海外荣耀
查看>>
磁盘空间满引起的mysql启动失败:ERROR! MySQL server PID file could not be found!
查看>>
点播转码相关常见问题及排查方式
查看>>
[arm驱动]linux设备地址映射到用户空间
查看>>
弗洛伊德算法
查看>>
【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
查看>>
精度 Precision
查看>>
Android——4.2 - 3G移植之路之 APN (五)
查看>>
Linux_DHCP服务搭建
查看>>
[SilverLight]DataGrid实现批量输入(like Excel)(补充)
查看>>
秋式广告杀手:广告拦截原理与杀手组织
查看>>
翻译 | 摆脱浏览器限制的JavaScript
查看>>
闲扯下午引爆乌云社区“盗窃”乌云币事件
查看>>
02@在类的头文件中尽量少引入其他头文件
查看>>
JAVA IO BIO NIO AIO
查看>>
input checkbox 复选框大小修改
查看>>