我正在尝试使用erlang + mnesia构建一个小型测试应用程序.
我有#user记录的用户表构建,如下所示:
-record(user_details, {name, password}).
-record(user, {id, details}).
Run Code Online (Sandbox Code Playgroud)
然后我插入一个具有该功能的用户:
add_sample_data() ->
Mat = #user{
details = #user_details{
name = "mat", password = "mat"
}
},
user:insert_user(Mat),
Run Code Online (Sandbox Code Playgroud)
查询[U#user.details || U <- mnesia:table(user)]返回非空列表.现在我正在尝试构建一个查询,如果没有details.name匹配名称或匹配记录(如果有),则返回包含零记录的列表.
这是我使用的方法(这个工作):
user_exists() ->
Fun = fun() ->
Query = qlc:q([
U#user.details ||
U <- mnesia:table(user)
]),
qlc:e(Query)
end,
case mnesia:transaction(Fun) of
{atomic, []} -> false;
{atomic, [_User]} -> true
end.
Run Code Online (Sandbox Code Playgroud)
我复制了本教程中的一些内容.mnesia:select在mne_fun_query({sport, Sport})方法(幻灯片19)中解决了类似的问题,但现在我想用qlc来做.
我尝试了各种组合但没有任何成功(通常在编译时失败..).
我是erlang的新手,如果你能分辨出哪个查询应该有效并稍微解释一下,我将不胜感激!
垫.
编辑
这是一个版本不起作用,但可能更好地解释我的问题
user_exists() -> …Run Code Online (Sandbox Code Playgroud) 我是ejabberd的全新人物.我已经下载了windows和linux(Ubuntu)的安装.在网络的某个地方,我看过如何安装和设置它的演示,但安装被配置为"演示".我有pidgin XMPP客户端,现在我在设置ejabberd时遇到了问题.
另一个问题是我试图在后端使用Nitrogen的彗星池和erlang ETS表开发聊天系统(由于现在嵌入在ets表中的故障转移机制),即
ets:give_away/3 [it gives my chat engines fault tolerance, thanks to the guys
who maintain the virtual machine who saw how useful this is]
Please point me in the right direction to get ejabberd up and running.
On the processOne Website and the ejabberd documentation i have still
failed to start it well. Please help
我有一个mnesia表t,其中包含具有单个字段的记录x.如何选择一个随机值x从t?
为了避免数学迂腐的整个过程:我不关心随机数生成的细节,我只是希望我的结果一般不会每次都相同.
谢谢,
-tjw
我有 5 个进程在 mnesia 表中插入/更新相同的 3 条记录。这些进程中的每一个都在单个事务中执行其插入/更新。
我还有 5 个其他进程读取这些相同的 3 条记录,也在一个事务中。
除非我将整个表锁定为多记录事务的一部分,否则我会收到 {aborted, {cyclic, node....}} 错误。我的直觉是,我的用例是普通的,其本身不应该导致事务中止。有人可以帮助我解决我愚蠢的想法吗?我所做的只是在一个事务中的缓存(mnesia 表)中插入(或读取)多行。
插入 3 条记录如下所示
insert_keylist(Keys) ->
F = fun() -> insert_iter(Keys) end,
transactional_execute(F).
insert_iter([]) ->
ok;
insert_iter([{Key, Value} | KVs]) ->
insert(Key, Value),
insert_iter(Kvs).
insert(Key, Value) ->
F =
fun() ->
case sc_store:lookup(Key) of
{ok, _Value} -> sc_store:replace(Key, Value);
{error, not_found} -> sc_store:insert(Key,Value)
end
end,
transactional_execute(F).
transactional_execute(F) ->
case mnesia:is_transaction() of
true -> F();
false ->
try mnesia:sync_transaction(F) of
{atomic, Result} -> Result; …Run Code Online (Sandbox Code Playgroud) 因此,我在一次交易中对记录集进行CRUD操作时遇到各种麻烦。它导致我在此处发布2个问题,Trouble和MoreTrouble。但是,我认为这两个问题都是由以下原因造成的:在我的事务中,我将mnesia:writes,reads等包含在try / catch块中,该块捕获了包括mnesia异常中止的事务在内的所有内容,并将其作为避免死锁的算法的一部分。即
insert(Key, Value) ->
F =
fun() ->
case sc_store:lookup(Key) of
{ok, _Value} -> sc_store:replace(Key, Value);
{error, not_found} -> sc_store:insert(Key,Value)
end
end,
try
case mnesia:transaction(F) of
{atomic, Result} -> Result;
{aborted, Reason} -> ...
end
catch
Error:Reason -> ...
end
Run Code Online (Sandbox Code Playgroud)
结束
例如,sc:lookup / 1看起来像这样:
lookup(Key) ->
try
case mnesia:read(key_to_value, Key) of
[#key_to_value{type = Type, scope = Scope, value = Value}] ->
{ok, {Value, Type, Scope}};
[] ->
{error, not_found}
end …Run Code Online (Sandbox Code Playgroud) 我有表用户
-record(person, {id, firstname, lastname}).
Run Code Online (Sandbox Code Playgroud)
此表包含以下值:
1 francoi mocci
2 test tes
Run Code Online (Sandbox Code Playgroud)
我想清除此表中的数据
我没有在erlang中找到语法来清除表用户的数据
我找到了删除表的功能
mnesia:delete(..)
Run Code Online (Sandbox Code Playgroud) Mnesia 有四种读取数据库的方法:read, match_object, select, qlc。当然,除了他们肮脏的同行。它们中的每一个都比以前的更具表现力。
更新。
正如我提到的CRAP ANSWERS,read只是一个键值查找,但经过一段时间的探索,我发现还有函数index_readand index_write,它们以相同的方式工作,但使用索引而不是主键。
如何在Erlang/OTP v17中查看mnesia表.我尝试使用tv:start()但shell返回undefined function.看起来tv模块不再存在.用什么代替它?
对于我的生活,无法弄清楚这一点。我正在尝试创建 mnesia 表,但不断收到这个奇怪的错误。
这是我的命令:
ok = mnesia:create_schema(Nodes),
rpc:multicall(Nodes, application, start, [mnesia]),
{_, ok} = mnesia:create_table(rr_events,
[{attributes, record_info(fields, rr_events)},
{index, [#rr_events.key]},
{disc_copies, Nodes}]),
rpc:multicall(Nodes, application, stop, [mnesia]).
Run Code Online (Sandbox Code Playgroud)
这是我的记录:
-record(rr_events, {key, events=[]}).
Run Code Online (Sandbox Code Playgroud)
这是错误:
=PROGRESS REPORT==== 24-Mar-2016::21:53:42 ===
application: mnesia
started_at: nonode@nohost
** exception error: no match of right hand side value
{aborted,{bad_type,rr_events,{index,[2]}}}
in function rr:install/1 (c:/Users/zzzz/Projects/zzz/zzz/rr/rr/_build/default/lib/rr/src/rr.erl, line 13)
Run Code Online (Sandbox Code Playgroud)
知道这可能是什么吗?无法弄清楚这一点。