提取对应于mnesia中的值的多行

时间:2015-02-27 06:50:36

标签: erlang ejabberd mnesia

情况是: - 我有mnesia元组,如{" Groupid(主键)"," groupname"," grouptype"," creatorid& #34;" adminid"" Member_list"}

Member_list =" memberone @ xyz,membertwo @ xyz,memberthree @ xyz"。现在我想提取所有那些存在membertwo @ xyz的行。如何从mnesisa中选择保护为此? ?任何指针

现在经过sherif给出的方法后出现错误

     in function  checktable1:getRecords/1 (checktable1.erl, line 201)

37> checktable1:getRecords("&#34)。 **异常错误:右侧值不匹配                     {中止,                         {民主基金,                             [{字符串,代币,[" A,B,C"""],[]},                              {checktable1,存在,2,                                  [{文件," checktable1.erl"},{线,203}]},                              {checktable1,' -getRecords / 1-乐趣-1 - ',7,                                  [{文件," checktable1.erl"},{线,197}]},                              {QLC,收集,1,[{文件," qlc.erl"},{线,1330}]},                              {QLC,EVAL,2,[{文件," qlc.erl"},{线,296}]},                              {mnesia_tm,apply_fun,3,                                  [{文件," mnesia_tm.erl"},{线,833}]},                              {mnesia_tm,execute_transaction,5,                                  [{文件," mnesia_tm.erl"},{线,813}]},                              {checktable1,getRecords,1,                                  [{文件," checktable1.erl"},{线,201}]}]}}      在函数checktable1中:getRecords / 1(checktable1.erl,第201行)

1 个答案:

答案 0 :(得分:1)

你将不得不使用mnemosyne

getRecords(ListMember)->  
    F = fun() ->
            Q = qlc:q(
                    [
                            Record
                            || Record <- mnesia:table(table_name_here),
                               exists(Record#table_name_here.member_list, ListMember)
                    ]),
            qlc:e(Q)
    end,
    {atomic, L}=mnesia:transaction(F),
    L.

然后你需要实现函数exists(Member_list,Member),它扫描成员的Member_list,如果找到则返回true,否则返回false。别忘了

-include_lib("stdlib/include/qlc.hrl"). 

这不符合,仅用于演示。我可能还建议您更改数据库设计以避免扁平列表(字符串形式的列表),或任何列表作为值。如果可能的话,我当然不知道你在做什么。你应该至少能够在那里放一个成员列表而不是一个字符串。这是mnesia你可以把任何东西放在任何表中。但这并不意味着你应该这样做。

编辑:

exists(ML, M)->lookUp(string:tokens(ML, ","), M).
lookUp([], M)->false;
lookUp([M|R], M)->true;
lookUp([_|R], M)->lookUp(R,M).

如果你想要,你也可以使用以下而不是exists(Record#table_name_here.member_list,ListMember)。

lists:member(ListMember, string:tokens(Record#table_name_here.member_list, ","))