Realm的入门使用

一.介绍

Realm是一个不错的手机平台上的数据库,支持多种编程环境,如:Java、Object-C、React Native、Swift、Xamari等。

Realm的官网:https://realm.io/

github地址:https://github.com/realm

realm-java库的文档:https://realm.io/docs/java/la...

二.使用

1)配置:

//1.工程gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        //1. 添加realm插件
        classpath "io.realm:realm-gradle-plugin:1.2.0"
    }
}

//2. app的gradle
apply plugin: 'realm-android'

dependencies {
    compile 'io.realm:realm-android-library:1.2.0'
}

2)数据块操作实体类

package com.peter.schoolmarket.data.pojo;

import java.io.Serializable;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.Required;

/**
 * Created by PetterChen on 2017/4/10.
 */

public class User extends RealmObject implements Serializable {
    @PrimaryKey //主键id
    private int id;//
    private String username;//用户名
    private String password;//用户密码
    private String phone;//手机号
    private String imgUrl;//头像地址

    public User() {
        //
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getPhone() {
        return phone;
    }

    public String getImgUrl() {
        return imgUrl;
    }
}

3)初始化

//Realm初始化,这是默认初始化,可以自定义初始化
Realm.init(this);//this表示上下文
RealmConfiguration config = new RealmConfiguration.Builder().build();//可以修改
Realm.setDefaultConfiguration(config);

4)数据库操作

可以使用事务或者事务块,对于事务块,有同步的和异步的,如果使用异步事务块,需要手动关闭

//增,事务增
Realm realm=Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealm(user);
realm.commitTransaction();

//增,事务块增
Realm  mRealm=Realm.getDefaultInstance();
mRealm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {

    realm.copyToRealm(user);

    }
});

//删,事务块删
Realm  mRealm=Realm.getDefaultInstance();
final RealmResults<Dog> dogs=  mRealm.where(Dog.class).findAll();
mRealm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {

        Dog dog=dogs.get(5);
        dog.deleteFromRealm();
        //删除第一个数据
        dogs.deleteFirstFromRealm();
        //删除最后一个数据
        dogs.deleteLastFromRealm();
        //删除位置为1的数据
        dogs.deleteFromRealm(1);
        //删除所有数据
        dogs.deleteAllFromRealm();
    }
});
//同理,也可以使用事务删

//查,查询全部
Realm  mRealm=Realm.getDefaultInstance();
RealmResults<Dog> dogs = mRealm.where(Dog.class).findAll();
List<Dog> list = mRealm.copyFromRealm(dogs);

//查,条件查询
Realm  mRealm=Realm.getDefaultInstance();
Dog dog = mRealm.where(Dog.class).equalTo("id", id).findFirst();

//改
Realm  mRealm=Realm.getDefaultInstance();
Dog dog = mRealm.where(Dog.class).equalTo("id", id).findFirst();
mRealm.beginTransaction();
dog.setName(newName);
mRealm.commitTransaction();

三.最后

这篇博文算不上是原创,都是参考其它博文写的,只是为了以后自己有用到Realm时,可以不用再去查资料。
参考博文:
http://blog.csdn.net/fwt336/a...
http://www.cnblogs.com/liushi...
http://www.jianshu.com/p/fdc9...

相关推荐