erlang mnesia php,Erlang Mnesia数据库简单导入导出

原型

如果并且当我们愿意启动和操控 Mnesia 的时候, 将定义和数据写到一个普通文本文件会比较容易一些。起初,既没有表也没有数据存在,也不知道需要什么样的表。在原型的初始阶段,把所有数据写到一个文件,处理这个文件并且将这个文件中的数据插入到数据库是很明智的。从一个文本文件读数据来初始化 Mnesia 是可能的。我们有下面这两个函数可以处理文本文件:

mnesia:load_textfile(Filename)。 这个函数加载一系列可在文件中找到的表定义和数据到 Mnesia。此函数也启动 Mnesia 并且有可能创建一个新模式。此函数仅在本地

节点操作。

mnesia:dump_to_textfile(Filename) 转储一个 Mnesia 系统的全部本地表到一个能够被普通文本编辑器编辑并且以后可以重新加载的文本文件中。

这些函数与Mnesia的普通存储和加载函数相比当然是非常慢,但主要用于小型实验系统和初始的原型,其好处是非常容易使用

文本文件的格式为:

{tables, [{Typename, [Options]},

{Typename2 ......}]}.

{Typename, Attribute1, Atrribute2 ....}.

{Typename, Attribute1, Atrribute2 ....}

Options是一个符合mnesia:create_table/2选项要求的{Key,Value}元组列表。

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

{tables,

[{fruit, [{attributes, [name, color, taste]}]},

{vegetable, [{attributes, [name, color, taste, price]}]}]}.

{fruit, orange, orange, sweet}.

{fruit, apple, green, sweet}.

{vegetable, carrot, orange, carrotish, 2.55}.

{vegetable, potato, yellow, none, 0.45}.

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

% erl

Erlang (BEAM) emulator version 4.9

Eshell V4.9 (abort with ^G)

1> mnesia:load_textfile("FRUITS").

New table fruit

New table vegetable

{atomic,ok}

2> mnesia:info().

---> Processes holding locks

---> Processes waiting for locks

---> Pending (remote) transactions

---> Active (local) transactions

---> Uncertain transactions

---> Active tables

vegetable : with 2 records occuping 299 words of mem

fruit : with 2 records occuping 291 words of mem

schema : with 3 records occuping 401 words of mem

===> System info in version "1.1", debug level = none <===

opt_disc. Directory "/var/tmp/Mnesia.nonode@nohost" is used.

use fallback at restart = false

running db nodes = [nonode@nohost]

stopped db nodes = []

remote = []

ram_copies = [fruit,vegetable]

disc_copies = [schema]

disc_only_copies = []

[{nonode@nohost,disc_copies}] = [schema]

[{nonode@nohost,ram_copies}] = [fruit,vegetable]

3 transactions committed, 0 aborted, 0 restarted, 2 logged to disc

0 held locks, 0 in queue; 0 local transactions, 0 remote

0 transactions waits for other nodes: []

ok

3>

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

摘自:Erlang mnesia官方手册

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值