Reputation: 755
Im new in Erlang and Mnesia. I created a table in 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().
Now i want to select all rows of table base where the "game" is a certain value. i wrote this function but it does not work when Game variable is not equal to "".
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;
How can i make my query to it as i want?
Upvotes: 0
Views: 218
Reputation: 755
Solved. I changed my function:
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;
Upvotes: 1