我不明白这里有什么问题,但我可能会误解语法.我正在尝试从我的"用户"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
用于从表中删除给定的记录.它不会根据匹配规范删除多个记录.它只是一个更精确的版本mnesia:delete/*
- 它为您提供了一种从包表中删除特定记录的方法.
要删除多个匹配规范的记录(例如,使用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).