Home Database Mysql Tutorial Mnesia数据库操作整理(1)

Mnesia数据库操作整理(1)

Jun 07, 2016 pm 03:42 PM
operate database tidy

Mnesia数据库的操作和以前习惯的SQL数据库操作几乎完全不同,刚开始用很不使用。现将常见的读写操作整理于下: 1、脏读 mnesia:dirty_read(Tab,Key)-ValueList | exit({aborted,Reason}) 示例: 对于set类型的表,这个操作返回一个或者空表,因为set类型表键

Mnesia数据库的操作和以前习惯的SQL数据库操作几乎完全不同,刚开始用很不使用。现将常见的读写操作整理于下:

1、脏读

mnesia:dirty_read(Tab,Key)->ValueList | exit({aborted,Reason})

示例:

Mnesia数据库操作整理(1)


对于set类型的表,这个操作返回一个值或者空表,因为set类型表键值不可重复。如果对bag类型(键值可重复,但不能同键又同值)用脏读,结果是一个或若干值的列表,一个值都没有就是个空表。

示例:

Mnesia数据库操作整理(1)


2、脏写

mnesia:dirty_write(Tab,Record) -> ok | exit({abouted,Reason})

示例:

Mnesia数据库操作整理(1)


3、读read

这个操作,只能用在事务中,所以也不能直接在shell里使用。

示例:

Mnesia数据库操作整理(1)


4、写write

同上,普通的写操作也必须在事务中完成,否则会报错。

示例:

Mnesia数据库操作整理(1)


小结:read和write都是用于事务的,在不使用事务的时候,直接用dirty_read和dirty_write。

另,以上2种读操作都是按照主键进行查询。


5、使用带匹配规范的mnesia:select

Mnesia数据库操作整理(1)

Mnesia数据库操作整理(1)

这个命令也必须放到事务中才能执行。第一个参数是要查询的表,第二个参数是匹配规范。是个{Head,Conditions,Results}格式的三元组。

Head是个Erlang项式,用于描述查询模式,其中的'$1','$2'等原子用于表示变量,其对应于记录中第一个、第二个字段,以此类推。


‘_’(仅仅限于在Head部分使用),表示无所谓,任意值都可以匹配,示例:

Mnesia数据库操作整理(1)


'$$'(仅仅限于Results和Conditions中使用),等价于罗列出依次在Head部分匹配的所有变量'$1','$1','$1'等,示例参见上图。这个主要用于我们只对查询结果的部分字段感兴趣的时候用,不需要把整条记录都选择出来。


'$_'(仅仅限于Results和Conditions中使用),表示与查询条件相匹配的整条记录,示例:

Mnesia数据库操作整理(1)


6、使用查询列表速构(QLC)

QLC的全称是Query List Comprehension。表面看,它类似于普通的列表速构,使用时,又必须嵌套于外观类似于函数调用的qlc:q()中使用。实际上外层的qlc:q(...)只是一个标记,用于让编译器区别对待其中的表达式。要启用该功能,模块源码中必须加上下面这一行:

-include_lib("stdlib/include/qlc.hrl").

但作为特例,可以直接在Erlang shell中使用qlc:q(...)。也可以在事务中使用QLC,任何可以在事务中使用的Mnesia函数都可以与QLC混合使用。


示例:

Mnesia数据库操作整理(1)

上图是仅仅获取users表中的acc字段。


Mnesia数据库操作整理(1)

这是获取users表中的整条记录。


有关QLC的更多复杂用法,可以参见Erlang之父Joe Armstrong的新作《Programming Erlang, 2nd》第20章,“Mnesia:The Erlang Database”,326页。其中涉及到比如“2个表联合查询”。


QLC在erl源码中的用法示例:

Mnesia数据库操作整理(1)


7、删除mnesia:delete

要从数据库删除一行,需要知道该行的对象ID(OID)。对象ID由一个由表名和主键构成的二元组。

示例:

Mnesia数据库操作整理(1)


8、更新操作

Mnesia数据表里没有UPDATE的概念,如果要修改某条记录,根据表的不同类型,可以先删除,再插入新的记录,或者直接添加新记录代替旧的记录。


9、事务操作

事务代码的结构:

Mnesia数据库操作整理(1)


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

How to bind WeChat on Ele.me How to bind WeChat on Ele.me Apr 01, 2024 pm 03:46 PM

Ele.me is a software that brings together a variety of different delicacies. You can choose and place an order online. The merchant will make it immediately after receiving the order. Users can bind WeChat through the software. If you want to know the specific operation method , remember to check out the PHP Chinese website. Instructions on how to bind WeChat to Ele.me: 1. First open the Ele.me software. After entering the homepage, we click [My] in the lower right corner; 2. Then in the My page, we need to click [Account] in the upper left corner; 3. Then come to the personal information page where we can bind mobile phones, WeChat, Alipay, and Taobao. Here we click [WeChat]; 4. After the final click, select the WeChat account that needs to be bound in the WeChat authorization page and click Just [Allow];

See all articles