Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5009282
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2012-09-04 15:45:33

假如我们开始玩一个关于健康食品的小型数据库,可输入下列数据到文件data.txt:

  1. {tables,[
  2.     {fruit, [{attributes, [name, color, taste]}]},
  3.     {vegetable, [{attributes, [name, color, taste, price]}]}
  4. ]}.

  5. {fruit, orange, orange, sweet}.
  6. {fruit, apple, green, sweet}.
  7. {vegetable, carrot, orange, carrotish, 2.55}.
  8. {vegetable, potato, yellow, none, 0.45}.

下列Erlang shell会话显示如何加载fruits数据库:


 

  1. $ erl
  2. Erlang (BEAM) emulator version 5.6.3 [source] [smp:2] [async-threads:0] [kernel-poll:false]Eshell V5.6.3 (abort with ^G)
  3. 1> mnesia:start().   % 虽然 mnesia:load_textfile 也会启动数据库,但是在处理上次遗留的内存数据库上有问题, 会保留上次非正常退出时的内存表的结构,造成本次 mnesia:load_textfile 操作失败。出现注1的错误,造成无法导入本 次的数据。
    ok
    2> mnesia:load_textfile("data.txt").
    New table fruit
    New table vegetable
    {atomic,ok}
    3> mnesia:info().
     ---> Processes holding locks <---
    ---> Processes waiting for locks <---
    ---> Participant transactions <---
    ---> Coordinator transactions <---
    ---> Uncertain transactions <---
     ---> Active tables <---
    vegetable      : with 2        records occupying 320      words of mem
    fruit          : with 2        records occupying 312      words of mem
    schema         : with 3        records occupying 622      words of mem
    ===> System info in version "4.4.3", debug level = none <===
    opt_disc. Directory "/home/gamexg/erl/mnesia/Mnesia.nonode@nohost" is NOT used.
    use fallback at restart = false
    running db nodes   = [nonode@nohost]
    stopped db nodes   = []
    master node tables = []
    remote             = []
    ram_copies         = [fruit,schema,vegetable]
    disc_copies        = []
    disc_only_copies   = []
    [{nonode@nohost,ram_copies}] = [schema,fruit,vegetable]
    5 transactions committed, 0 aborted, 0 restarted, 0 logged to disc
    0 held locks, 0 in queue; 0 local transactions, 0 remote
    0 transactions waits for other nodes: []
    ok
    4>


这里我们能够看到怎样从一个规范的文本文件来初始化数据库管理系统。

注1:不使用mnesia:start 启动数据库直接载入数据会使上次运行的内存表未被清除,而和本次导入的表冲突,导入失败。


 

$ erl

Erlang (BEAM) emulator version 5.6.3 [source] [smp:2] [async-threads:0] [kernel-poll:false]

    Eshell V5.6.3 (abort with ^G)
    1> mnesia:load_textfile("data.txt").
    ** Table fruit already exists on nowhere, just entering data
    ** Table vegetable already exists on nowhere, just entering data
    {aborted,{no_exists,fruit}}
    2>



    原文:

    导入测试数据到-mnesia-数据库/



     

    阅读(1480) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~