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. [解决方案][错误代码:0x80070002]IIS7及以上伪静态报错404

    故障现象:DTCMS开启伪静态功能,VS2010预览正常,发布到IIS后报错404.0错误 (WIN7,WIN8,SERVER2008).模块IISWebCore通知MapRequestHandler ...

  2. docker (centOS 7) 使用笔记3 - 修改docker默认的虚拟网址

    近日在使用VPN时发现和docker的虚拟网址发生了冲突,都是172.17.0.1,故需要修改docker的默认网址. 1. 当前状态 # ifconfig docker0: flags=<UP ...

  3. 配置vscode使它能够在自定义扩展名当中支持emment语法

    在.vue文件当中默认是不支持emment的,需要在vscode设置当中设置 "emmet.syntaxProfiles": { "vue-html": &qu ...

  4. spring boot-html和templates

    静态页面 spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下 /static /public /resources / ...

  5. 洛谷 [P2886] 牛继电器Cow Relays

    最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include ...

  6. luogu 3709 大爷的字符串题 构造 莫队 区间众数

    题目链接 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区间中随机拿出一个字符\(x\),然后把\(x\)从这个区间中删除,你要维护一个集合S 如果\(S\)为空,你\(r ...

  7. 转 Python爬虫入门一之综述

    转自: http://cuiqingcai.com/927.html 静觅 » Python爬虫入门一之综述 首先爬虫是什么? 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为 ...

  8. php自动获取字符串编码函数mb_detect_encoding

    当在php中使用mb_detect_encoding函数进行编码识别时,很多人都碰到过识别编码有误的问题,例如对与GB2312和UTF- 8,或者UTF-8和GBK(这里主要是对于cp936的判断), ...

  9. Integration testing

    Integration testing 集成测试用来确保app的不同模块之间可以正确的一起工作.ASP.NET Core提供单元测试框架和内建的测试网络服务来支持集成测试,并且测试网络服务不需要网络开 ...

  10. GitHub 上受欢迎的 Android UI Library 整理(一)

    抽屉菜单 https://github.com/mikepenz/MaterialDrawer ★7337 - 安卓抽屉效果实现方案https://github.com/Yalantis/Side-M ...