| Author |
Message |
< Erlang ~ New to Erlang: problem with mnesia update query |
| LRP |
Posted: Wed Oct 08, 2008 8:15 pm |
|
|
|
Joined: 08 Oct 2008
Posts: 8
Location: Boston, MA.
|
Hello,
Following the pattern in Getting Started with Mnesia:
http://www.erlang-fr.org/erlang.org/lib/mnesia-4.1/doc/html/Mnesia_chap2.html
I've written the following update query in my news server
updateHead(NewsID, Head) ->
%% update news item headline
mnesia:transaction(
fun() ->
[R] = mnesia:read({news, NewsID, write}),
New = R#news{head = Head},
mnesia:write(New)
end ).
Compiles fine. But doesn't update.
No doubt I'm doing something dumb. Can some kind soul please steer me right?
Here are the related elements in my server source:
handle_call({update_head, NewsID, Head}, _From, State) ->
updateHead(NewsID, Head),
{reply, ok, State};
updateNewsHead(NewsID, Head) ->
gen_server:call(?SERVER, {update_head, NewsID, Head}).
-export([start_link/0, insertNewsItem/2, updateNewsHead/2, listNews/0, selectNewsItem/1, selectNewsByDate/2, shutdown/0]).
Table def:
init_newsStore() ->
% Initialize database
mnesia:create_schema([node()]),
mnesia:start(),
try
mnesia:table_info(news, type)
catch
exit: _ ->
mnesia:create_table(news, [
{attributes, record_info(fields, news)},
{type, set},
{disc_copies, [node()] }
])
end.
Comment: Gee I wish there was one mnesia tutorial with a half a dozen or so SIMPLE examples each of insert/select/update/delete queries. I've been scrambling all over the net to figure out how to write this one function.
Many thanks in advance,
LRP |
|
|
| Back to top |
|
| Roux Viljoen |
Posted: Tue Nov 04, 2008 11:46 am |
|
|
|
Joined: 04 Nov 2008
Posts: 3
Location: London
|
LRP,
I believe you used the mnesia:read/3 incorrectly.
Code:
[R] = mnesia:read({news, NewsID, write}),
instead of...
Code:
[R] = mnesia:read(news, NewsID, write),
Once its called correctly you shouldn't have any more problems except if you have no entries that match with the NewsID.
To prevent yourself from getting an error, you should use a "Case" to cater for a [] return.
Hope that helps  |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|