原文地址: 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 < 250
demo(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).

[erlang] mnesia的更多相关文章

  1. erlang mnesia 数据库实现SQL查询

    Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南.下面的内容将着重说 ...

  2. erlang mnesia数据库设置主键自增

    Mnesia是erlang/otp自带的分布式数据库管理系统.mnesia配合erlang的实现近乎理想,但在实际使用当中差强人意,总会有一些不足.mnesia数据表没有主键自增的功能,但在mnesi ...

  3. erlang mnesia 数据库查询

    Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南.下面的内容将着重说 ...

  4. erlang mnesia数据库简单应用

    mnesia是erlang自带的分布式数据库,基于ets和dets实现的.mnesia兼顾了dets的持久性和ets的高性能,可以自动在多个erlang节点间同步数据库.最关键的是,mnesia实现了 ...

  5. [Erlang]Mnesia分布式应用

    http://blog.csdn.net/erlib/article/details/40743687 情景: 设计一个图书管理系统,需求: 1. 基本的增删查改功能; 2. 支持多节点备份(其中一个 ...

  6. erlang 分布式数据库Mnesia 实现及应用

    先推荐一篇:mnesia源码分析(yufeng)   - linear hash   ETS/DETS/mnesia 都使用了linear hash算法 http://en.wikipedia.org ...

  7. [Erlang 0119] Erlang OTP 源码阅读指引

      上周Erlang讨论群里面提到lists的++实现,争论大多基于猜测,其实打开代码看一下就都明了.贴出代码截图后有同学问这代码是哪里找的?   "代码去哪里找?",关于Erla ...

  8. [Erlang 0115] 2014值得期待的Erlang两本新书

    在2014年的开头就有这样一个令人振奋的好消息,Erlang有一本新书即将出版 <The Erlang Runtime System>,其作者happi在2013年3月份公布了这本书的写作 ...

  9. WhatsApp的Erlang世界

    rick 的两个ppt整理 下载:2012 2013  ,使用半年erlang后,重新看这两个ppt才发现更多值的学习的地方,从ppt中整理如下: - Prefer os:timestamp to e ...

随机推荐

  1. [转]html网页 swf播放器使用代码

    <object id="player" height="240" width="275" classid="CLSID:6B ...

  2. jexus docker

    一.准备工作 1.init.sh 文件 #!/bin/bash # Stop your services function stop_svc { /usr/jexus/jws stop >/de ...

  3. Win7下如何使用GCC编译器

    很多Linux的爱好者都很熟悉GCC编译器,但是对面初学者,如何去学习GCC使用GCC ,很多人都是直接在电脑上装一个虚拟机,这样不仅安装麻烦,而且占用了很多电脑资源,今天我来教大家如何在Win7使用 ...

  4. MySQL Replication Report

    很多人都会MySQL主从框架的搭建,但很多人没有真正理解同步基本用途.同步的基本原理,还有当Master和Slave同步断开后的处理以及导致Master和slave不同步的原因等等,当你对这些都了如指 ...

  5. 浙江省“一卡通”异地就医,C#调用省一卡通动态库

    前言,最近学习调用 浙江省一卡通业务,主要就是调用一个DLL,动态库文件,这个动态库是浙大网新研发的. 借着自学的机会把心得体会都记录下来,方便感兴趣的小伙伴学习与讨论. 内容均系原创,欢迎大家转载分 ...

  6. zabbix 自动发现

    转自:https://blog.csdn.net/yyy72999/article/details/76065374 zabbix自动发现/zabbix自动发现规则 置顶2017年07月25日 14: ...

  7. myBatsi调用存储过程

    1.结构 2.准备数据 建表和插入数据 CREATE TABLE p_user( id INT PRIMARY KEY AUTO_INCREMENT, name ), sex ) ); INSERT ...

  8. linux学习笔记-12.输入输出重定向及管道

    1.新建一个文件 touch a.txt> b.txt 2.错误重定向:2> find /etc -name zhaoxing.txt 2> error.txt 3.将正确或错误的信 ...

  9. 通俗讲解transform3D变换时css各属性的作用与搭配

    当没有浏览器兼容性限制时,就大胆地使用transiton的3D效果吧,前端也要做不一样的烟火! *常用的3D效果 rotateX/rotateY/rotateZ/rotate3dtranslateX/ ...

  10. 发布Web端

    1.右键发布 2.配置文件,选择自定义 3.填写配置名称 4.选择本地目录 5.最后发布