问题描述
情况是: - 我有像{Groupid(主键),groupname,grouptype,creatorid,adminid,Member_list}之类的元素元组。Member_list =memberone @ xyz,membertwo @ xyz,memberthree @ xyz。现在我想提取所有这些行,其中membertwo @ xyz存在。如何从mnesisa中选择这个?任何指针
现在,经过谢里夫给出的方法出现错误后,出现
in function checktable1:getRecords / 1(checktable1.erl,line 201)
37> checktable1:getRecords( A)。
**异常错误:右侧不匹配
{aborted,
{undef,
[{strings,tokens,[a,b,c, ,],[]},
{checktable1,exists,2,
[{file,checktable1.erl},{line,203}]},
{checktable1, --getRecords / 1-fun-1 - ',7,
[{file,checktable1.erl},{line,197}]},
{qlc,collect,1,[{file ,qlc.erl},{line,1330}]},
{qlc,eval,2,[{file,qlc.erl},{line,296}]},
{mnesia_tm,apply_fun,3,
[{file,mnesia_tm.erl},{line,833}]},
{mnesia_tm,execute_transaction,5,
[{file, mnesia_tm.erl},{line,813}]},
{checktable1,getRecords,1,
[{file,checktable1.erl},{line,201}]}]}}
在函数checktable1中:getRecords / 1(checktable1.erl,第201行)
你将不得不使用mnemosyne
getRecords(ListMember) >
F = fun() - >
Q = qlc:q(
[
记录
||记录< - mnesia:table(table_name_here),
存在(Record#table_name_here.member_list,ListMember )
qlc:e(Q)
end,
{atomic,L} = mnesia:transaction(F),
L.
然后需要实现功能存在(Member_list,Member),它会扫描会员的Member_list,如果找到,返回true否则为false。不要忘记 -include_lib(stdlib / include / qlc.hrl)。
这没有被遵守,它只用于演示。可能我还建议您更改数据库设计,以避免扁平化的列表(以字符串的形式列出),或者作为值的任何列表。如果可能的话,我当然不知道你在做什么。你应该至少能够在那里放置一个成员列表,而不是一个字符串,这是mnesia,你可以把任何地方在任何表中。不是意味着你应该。
编辑:
存在(ML,M) - > lookUp(string:tokens(ML,,),M)。
lookUp([],M) - > false;
查询([M | R],M) - > true;
lookUp([_ | R],M) - > lookUp(R,M)。
如果您希望您也可以使用以下而不是存在(Record#table_name_here.member_list,ListMember)
列表:member(ListMember,string:tokens(Record#table_name_here.member_list,,))
Situation is:- i have mnesia tuples like {"Groupid(Primary key)","groupname","grouptype","creatorid","adminid","Member_list"}.
Member_list="memberone@xyz,membertwo@xyz,memberthree@xyz".Now i want to extract all those rows in which membertwo@xyz exists.How to apply guard while selecting from mnesisa for this??Any pointers
Now after going through the approach given by sherif following error is appearing
in function checktable1:getRecords/1 (checktable1.erl, line 201)
37> checktable1:getRecords("a").** exception error: no match of right hand side value {aborted, {undef, [{strings,tokens,["a,b,c",","],[]}, {checktable1,exists,2, [{file,"checktable1.erl"},{line,203}]}, {checktable1,'-getRecords/1-fun-1-',7, [{file,"checktable1.erl"},{line,197}]}, {qlc,collect,1,[{file,"qlc.erl"},{line,1330}]}, {qlc,eval,2,[{file,"qlc.erl"},{line,296}]}, {mnesia_tm,apply_fun,3, [{file,"mnesia_tm.erl"},{line,833}]}, {mnesia_tm,execute_transaction,5, [{file,"mnesia_tm.erl"},{line,813}]}, {checktable1,getRecords,1, [{file,"checktable1.erl"},{line,201}]}]}} in function checktable1:getRecords/1 (checktable1.erl, line 201)
解决方案 you will have to use 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.
you then need to implement function exists(Member_list, Member) which scans the Member_list for the member and returns true if found and false otherwise. dont forget to
-include_lib("stdlib/include/qlc.hrl").
This was not complied, its only for demonstration. Might i also suggest you change your database design to avoid flattened lists (lists in the form of strings), or any list for that matter as a value. if possible of course, i do not know what you`re doing. You should at least be able to put a list of members there rather than a string, This is mnesia you can put anything anywhere in any table. Does not mean you should though.
edit:
exists(ML, M)->lookUp(string:tokens(ML, ","), M).
lookUp([], M)->false;
lookUp([M|R], M)->true;
lookUp([_|R], M)->lookUp(R,M).
if you want you could also use the following instead of exists(Record#table_name_here.member_list, ListMember).
lists:member(ListMember, string:tokens(Record#table_name_here.member_list, ","))
这篇关于提取与mnesia中的值相对应的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!