September 2013
Intermediate to advanced
548 pages
12h 25m
English
Again, we’ll assume we have created our database and defined a
shop table. Now we want to add or remove a row from the table.
We can add a row to the shop table as follows:
| test_mnesia.erl | |
| | add_shop_item(Name, Quantity, Cost) -> |
| | Row = #shop{item=Name, quantity=Quantity, cost=Cost}, |
| | F = fun() -> |
| | mnesia:write(Row) |
| | end, |
| | mnesia:transaction(F). |
This creates a shop record and inserts it
into the table.
| | 1> test_mnesia:start(). |
| | ok |
| | 2> test_mnesia:reset_tables(). |
| | {atomic, ok} |
| | %% list the shop table |
| | 3> test_mnesia:demo(select_shop). |
| | [{shop,orange,100,3.80000}, |
| | {shop,pear,200,3.60000}, |
| | {shop,banana,420,4.50000}, |
| | {shop,potato,2456,1.20000}, |
| | {shop,apple,20,2.30000}] |
| |
Read now
Unlock full access