- 论坛徽章:
- 0
|
其它参考:ets和record无逢结合
ets默认创建的为set类型表,既每条记录关健字唯一的表,但很多时候创建一个表有多个相同的关健字,如一个类似sns好友对应表:
uid friendId
-------------
101 201
101 202
101 203
....
初始化一个record,在文件 t.hrl 中:
-record(t, {uid,friendId}).
引入Eshell
>rr("t.hrl").
[t]
这时我们需要一个bag类型的ets表,以uid做主键:
>ets:new(t,[public,bag,named_table,{keypos, #t.uid}]).
t
在uid=101的关键字下新增记录:
>ets:insert(t,#t{uid=101,friendId=201}),
ets:insert(t,#t{uid=101,friendId=202}),
ets:insert(t,#t{uid=101,friendId=203}).
true
查看uid=101的所有记录:
>ets:lookup(t,101).
[#t{uid = 101,friendId = 201},
#t{uid = 101,friendId = 202},
#t{uid = 101,friendId = 203}]
删除friendId=202的记录
>ets:delete_object(t,#t{uid=101,friendId=202}).
true
再次查看是否已经删除
>ets:lookup(t,101).
[#t{uid = 101,friendId = 201},#t{uid = 101,friendId = 203}]
ok,friendId=202的记录已经删除. |
|