在复杂的mnesia表中选择一个值

时间:2015-08-30 04:54:58

标签: erlang mnesia

我是Erlang和Mnesia的新人。我在mnesia中创建了一个表:

-record(user, {name="", game=""}).
-record(base, {user=#user{}, score=0}).


init() ->
    mnesia:create_schema([node()]),
    mnesia:start(),
    mnesia:create_table(base, [{attributes, record_info(fields, base)}]),
    mnesia:stop().

现在我想选择“游戏”是特定值的所有表格行。我写了这个函数但是当Game变量不等于“”时它不起作用。

exec(base, Game) when is_list(Game)->
F = fun() ->
        if
            Game =/= "" ->
                Q = qlc:q([{R#base.user#user.name, R#base.score} || R <- mnesia:table(base), R#base.user#user.game == Game]);
            true ->
                Q = qlc:q([{R#base.user, R#base.score} || R <- mnesia:table(base)])
        end,
        qlc:e(Q)
    end,
{_, Data} = mnesia:transaction(F),
Data;

如何根据需要对其进行查询?

1 个答案:

答案 0 :(得分:1)

解决。我改变了我的功能:

exec(base, Game) when is_list(Game)->
    F = fun() ->
            if
                Game =/= "" ->
                    Q = qlc:q([{Name, Score} || #base{user={Name, G}, score=Score} <- mnesia:table(base), G == Game]);
                true ->
                    Q = qlc:q([{R#base.user, R#base.score} || R <- mnesia:table(base)])
            end,
            qlc:e(Q)
        end,
    {_, Data} = mnesia:transaction(F),
    Data;