mnesia如何修改表結構

來源:互聯網
上載者:User

標籤:des   style   blog   http   使用   資料   

mnesia建立的時候需要指定表record結構,如果定義的record結構修改了,就要更新資料的表結構,否則mnesia無法正常讀取和寫入資料。

我們最開始是這樣定義結構的

-record(person, {name, age}).

然後這樣建表

mnesia:create_table(person, [{disc_only_copies, nodes()}, {attributes, record_info(fields,person)}]).


有一天我們改變了這個person結構

-record(person, {name, age, money}).

我們還可以讀出mnesia的資料,但是不能匹配到現在的person結構了

> mnesia:dirty_read(person, lucy).
[{person,lucy,1}]

而且我們無法再寫入資料

> mnesia:dirty_write(person, #person{name = lily, age = 2, money = 0}).
** exception exit: {aborted,{bad_type,#person{name=lily,age=2, money=0}}}
     in function  mnesia:abort/1 (mnesia.erl, line 309)

但是我們還想要使用這個資料庫,可以這樣修改資料庫

Fun = fun(X)  ->
  case X of
      {person, Name, Age} ->
           {person, Name, Age, 0};
       _ ->
           X
     end
end,
NewAttr = [name, age, money],
mnesia:transform_table(person, Fun, NewAttr, person).


參考:

http://blog.csdn.net/mycwq/article/details/30101659

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.