gpt4 book ai didi

erlang - mnesia delete_object 异常?

转载 作者:行者123 更新时间:2023-12-02 05:55:56 24 4
gpt4 key购买 nike

我看不出这里有什么问题,但我可能只是误解了语法。我正在尝试从我的“用户”mnesia 表中删除名字为“ryan”(有多个)的“用户”记录。这是我所做的:

Delete=#user{first_name = "ryan", _ = '_'},
mnesia:dirty_delete_object(Delete)

这是我的记录定义:

-record(user, {id,
username,
password,
email,
first_name,
last_name,
last_login, % Date/time user last logged in
reg_date}). % Date/time user registered the account

这是我遇到的异常:

** exception exit: {aborted,
{bad_type,user,
{user,'_','_','_','_',"ryan",'_','_','_'}}}
in function mnesia:abort/1
in call from users:register/1

这看起来是绝配。是什么导致了这个问题?

最佳答案

mnesia:delete_object/1/3 用于从表中删除given 记录。它不会根据匹配规范 删除多条记录。它只是 mnesia:delete/* 的更精确版本 - 它为您提供了一种从 bag 表中删除特定 记录的方法。

要删除多个匹配规范的记录(例如使用 name="ryan"),请使用以下代码:

Delete=#user{first_name = "ryan", _ = '_'},
List = mnesia:dirty_match_object(Delete)
lists:foreach(fun(X) ->
mnesia:dirty_delete_object(X)
end, List).

或者它的交易版本:

Delete=#user{first_name = "ryan", _ = '_'},
Fun = fun() ->
List = mnesia:match_object(Delete)
lists:foreach(fun(X) ->
mnesia:delete_object(X)
end, List)
end,
mnesia:transaction(Fun).

关于erlang - mnesia delete_object 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com