之前提到了数据结构,现在把数据库的创建代码提供,没什么好说的,简单的查下资料,基本上什么都明白了。
-module( schema ) .
-export( [install / 1, install / 0 ] ) .
-include( "schema.hrl" ) .
install( Nodes ) when is_list( Nodes ) ->
mnesia:stop(),
mnesia:delete_schema( Nodes ),
catch( mnesia:create_schema( Nodes ) ) ,
mnesia:start(),
%% counter
case mnesia:create_table( counter,
[
{ disc_copies, Nodes },
{ type, set },
{ attributes, record_info( fields, counter ) }
] ) of
{ atomic, ok } ->
ok;
Any ->
error_logger:error_report( [
{message, "Cannot install table" },
{ table, counter },
{ error, Any },
{ nodes, Nodes } ] )
end,
%%game
case mnesia:create_table( game, [
{ disc_copies, Nodes },
{ index, [name,enable,test_mode] },
{ type, set },
{ attributes, record_info( fields, game ) },
{record_name, game}
] ) of
{ atomic, ok } ->
ok;
Any1 ->
error_logger:error_report( [
{ message, "Cannot install table" },
{ table, game },
{ error, Any1 },
{ nodes, Nodes } ] )
end,
%% player
case mnesia:create_table( player, [
{ disc_copies, Nodes },
{ index, [nick] },
{ type, set },
{ attributes, record_info( fields, player ) }
] ) of
{ atomic, ok } ->
ok;
Any2 ->
error_logger:error_report( [
{ message, "Cannot install table" },
{ table, player },
{ error, Any2 },
{ nodes, Nodes } ] )
end.
install() ->
install( [node()] ).