Mnesia基本用法

查看表结构

查看mnesia表的结构:

mnesia:info().

查看此表的基本信息:

mnesia:table_info(<tableName>, all).

Mnesia初使化

mnesia:stop(), mnesia:create_schema([node()]), mnesia:start().

创建表

mnesia:create_table(<tableName>, [{attributes, record_info(fields,<tableName>)}, {disc_copies, [node()]}]).

读表记录

读出表的所有key列表:

mnesia:dirty_all_keys(<tableName>).

根据key读表记录:

mnesia:dirty_read(<tableName>, <Key>).

写表记录

mnesia:dirty_write(<tableName>, {<tableName>, <ColumnValue1>, <ColumnValue2>, ...}).

删除表记录

mnesia:dirty_delete(<tableName>, <Key>).

其他实用操作

把表<table>的存储方式改成disc_copies:

mnesia:change_table_copy_type(dictionary, node(), disc_copies).

检测是否能ping通结点:

net_adm:ping('b@localhost').

查看内存使用情况:

memory().

ets表 与mnesia ram表的区别: mnesia ram就是基于ets的; 但mnesia ram支持事务,ets不支持.


-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_table(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).
Eshell V5.8.4  (abort with ^G)
1> c(test_mnesia).
{ok,test_mnesia}
2> test_mnesia:init().
stopped =INFO REPORT==== 16-May-2012::17:24:29 ===
application: mnesia
exited: stopped
type: temporary
3> test_mnesia:start().
ok
4> test_mnesia:reset_tables().
{atomic,ok}
5> test_mnesia:demo(select_shop).
[{shop,potato,2456,1.2},
{shop,orange,100,3.8},
{shop,apple,20,2.3},
{shop,pear,200,3.6},
{shop,banana,420,4.5}]
6> test_mnesia:demo(select_some).
[{potato,2456},
{orange,100},
{apple,20},
{pear,200},
{banana,420}]
7> test_mnesia:demo(where).
[{shop,orange,100,3.8},
{shop,apple,20,2.3},
{shop,pear,200,3.6}]
8> test_mnesia:demo(join).
[{shop,apple,20,2.3}]
9> test_mnesia:add_shop_item(apple,236,2.8).
{atomic,ok}
10> test_mnesia:demo(select_shop).
[{shop,potato,2456,1.2},
{shop,orange,100,3.8},
{shop,apple,236,2.8},
{shop,pear,200,3.6},
{shop,banana,420,4.5}]
11> test_mnesia:add_shop_item(egg,236,2.8).
{atomic,ok}
12> test_mnesia:demo(select_shop).
[{shop,potato,2456,1.2},
{shop,egg,236,2.8},
{shop,orange,100,3.8},
{shop,apple,236,2.8},
{shop,pear,200,3.6},
{shop,banana,420,4.5}]
13> test_mnesia:delete_shop_item(egg).
** exception error: undefined function test_mnesia:delete_shop_item/1
14> test_mnesia:remove_shop_item(egg).
{atomic,ok}
15> test_mnesia:demo(select_shop).
[{shop,potato,2456,1.2},
{shop,orange,100,3.8},
{shop,apple,236,2.8},
{shop,pear,200,3.6},
{shop,banana,420,4.5}]
16> test_mnesia:former(50).
{atomic,ok}
17> test_mnesia:demo(select_shop).
[{shop,potato,2456,1.2},
{shop,orange,50,3.8},
{shop,apple,336,2.8},
{shop,pear,200,3.6},
{shop,banana,420,4.5}]
18> test_mnesia:former(100).
{aborted,oranges}
19> test_mnesia:demo(select_shop).
[{shop,potato,2456,1.2},
{shop,orange,50,3.8},
{shop,apple,336,2.8},
{shop,pear,200,3.6},
{shop,banana,420,4.5}]
20> test_mnesia:add_plans().
{atomic,ok}
21> test_mnesia:get_plans(a).
{atomic,[]}
22> test_mnesia:get_plans(fred).
{atomic,[{design,fred,{rectangle,[10,5]}}]}
23> test_mnesia:get_plans(joe).
{atomic,[]}
24>

mnesia练习及基本操作的更多相关文章

  1. Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  2. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  3. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  4. 三、Redis基本操作——List

    小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...

  5. 二、Redis基本操作——String(实战篇)

    小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...

  6. 一、Redis基本操作——String(原理篇)

    小喵的唠叨话:最近京东图书大减价,小喵手痒了就买了本<Redis设计与实现>[1]来看看.这里权当小喵看书的笔记啦.这一系列的模式,主要是先介绍Redis的实现原理(可能很大一部分会直接照 ...

  7. Linq查询基本操作

    摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...

  8. C++ map的基本操作和使用

    原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...

  9. python之最强王者(10)———文件(File)、输入输出的基本操作

    1. Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 2.打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...

随机推荐

  1. 刷题总结——怪题(ssoj费用流)

    题目: 题目描述 给出一个长度为 n 的整数序列 hi ,现在要通过一些操作将这个序列修改为单调不降序列,即  hi≤hi+1 . 可以用的操作有 m 种,第 i 种操作可以通过支付 ci 的代价将一 ...

  2. [SPOJ-PT07J] Query on tree III (主席树)

    题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数 ...

  3. UVA 10003 Cutting Sticks(区间dp)

    Description    Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company ...

  4. 应用gulp工具构建个自动算rem布局的小例子

    因为最近可能需要做移动端rem布局,因为rem布局需要将px转化成rem,如果次都需要拿计算器算就太low了,所以就想到用less和gulp. 因为也是初学gulp,站点的文件结构还没想到太好,也只是 ...

  5. unity3d的模型规范

    1.不能存在单独的点和面,重复的面和点,不能被玩家所看到的模型也不必制作. 2.贴图要保证始终是2的倍数,但不必为正方形. 3.移动平台,如IP4,应该每个模型一般不超过300-1500个面,同屏不应 ...

  6. react-hot-loader 的配置【局部刷新】--{create-react-app}

    安装 1.安装create-react-app npm install -g create-react-app 2.创建项目 create-react-app my-app 配置 1.弹出配置文件 n ...

  7. ruti

    也许我可以说除了我把+号写成了-号这个程序几乎是完美的

  8. 51Nod 约数之和

                              1220 约数之和                                  题目来源: Project Euler 基准时间限制:3 秒 ...

  9. IntelliJ IDEA版本:Ultimate、Community、EAP版本的区别

    Community: 社区版,免费,但是功能有限制,Android Studio就是基于这个版本定制的. http://idea-intellij.com/intellij-community/ Ul ...

  10. 深入理解Activity启动流程(三)–Activity启动的详细流程2

    本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--A ...