Android Realm数据库使用

当我们的app有数据需要保存到本地缓存时,可以使用file,sharedpreferences,还有sqlite。

sharedpreferences其实使用xml的方式,以键值对形式存储基本数据类型的数据。对于有复杂筛选查询的操作,file和sharedpreferences都不能满足了。sqlite可以满足有大量复杂查询要求的缓存数据操作。但是sqlite的使用略复杂,代码量很大,还好网上有很多优秀的orm框架可使用,比喻ORMlite,greenDao等。

ORMlite,greenDao这些框架都是在SQLite的基础上封装的ORM对象关系映射框架,简化了代码操作。

而今天的主角:Realm是一个可以替代SQLite以及ORM Libraries的轻量级数据库。

Realm官网上说了好多优点,我觉得选用Realm的最吸引人的优点有三点:

  1. 跨平台: 现在很多应用都是要兼顾iOS和Android两个平台同时开发。如果两个平台都能使用相同的数据库,那就不用考虑内部数据的架构不同,使用Realm提供的API,可以使数据持久化层在两个平台一无差异化的转换
  2. 简单易用: Core Data 和 SQLite 几余、繁杂的知识和代码足以下退绝大多数刚入门的开发者,而换用 Realm,则可以极大地学习成本,立即学会本地化存储的方法。毫不吹嘘的说,把官方最新文档完看一遍,就完全可以上手开发了
  3. 可视化: Realm 还提供了一个轻量级的数据库查看工具,在Mac Appstore 可以下载“Realm Browser”这个工具,开发者可以查看数据库当中的内容,执行简单的插入和删除数据的操作。毕竟,很多时,开发者使用数据库的理由是因为要提供一些所谓的“知识库”

相比SQLite,Realm更快并且具有很多现代数据库的特性,比如支持JSON,流式api,数据变更通知,以及加密支持,这些都为安卓开发者带来了方便。不多介绍,更详细的介绍参见官网:Realm Home | Realm.io

我们重点来说说Reaml的使用,看看到底爽在哪里。

环境配置:

1、在Project的build.gradle文件中添加依赖:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:10.11.1"
    }
}

2、在app module的build.gradle文件的top添加下面代码:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

配置完毕。

使用:

在整个使用的过程中,Realm是主角,我们先来看看Realm类中的一段翻译:

/**
     * Realm类可以对你的持久化对象进行存储和事务管理,可以用来创建RealmObjects实例。领域内的对象可以在任何时间查询和读取。
     * 创建,修改和删除等操作必须被包含在一个完整的事务里面,后面的代码会讲到。
     * 该事务确保多个实例(在多个线程)可以在一个一致的状态和保证事务在ACID前提下,访问相同的对象。
     *
     * 当一个Realm实例操作完成后,切记不要忘记调用close()方法。否则会导致本地资源无法释放,引起OOM。
     *
     * Realm实例不能在不同的线程间访问操作。确切的说,你必须在每个要使用的线程上打开一个实例
     * 每个线程都会使用引用计数来自动缓存Realm实例,所以只要引用计数不达到零,
     *  调用getInstance(RealmConfiguration)方法将会返回缓存的Realm实例,应该算是一个轻量级的操作。
     *
     * 对于UI线程来说,打开和关闭Realm实例,应当放在onCreate/onDestroy或者onStart/onStop方法中
     *
     *  在不同的线程间,Realm实例使用Handler机制来调整他的状态。也就是说,Realm实例在线程中,如果没有Looper,是不能收到更新通知的,
     *  除非手动调用waitForChange()方法
     *
     * 在安卓Activity领域工作的一个标准模式可以在下面看到
     * 在Android Activity中,Realm的标准工作模式如下:
     *
     *
     * public class RealmApplication extends Application {
     *
     *     \@Override
     *     public void onCreate() {
     *         super.onCreate();
     *
     *         // The Realm file will be located in package's "files" directory.
     *         RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
     *         Realm.setDefaultConfiguration(realmConfig);
     *     }
     * }
     *
     * public class RealmActivity extends Activity {
     *
     *   private Realm realm;
     *
     *   \@Override
     *   protected void onCreate(Bundle savedInstanceState) {
     *     super.onCreate(savedInstanceState);
     *     setContentView(R.layout.layout_main);
     *     realm = Realm.getDefaultInstance();
     *   }
     *
     *   \@Override
     *   protected void onDestroy() {
     *     super.onDestroy();
     *     realm.close();
     *   }
     * }
     *
     *
     * Realm支持String和byte字段长度高达16MB
     * 参考连接:
     * <a href="http://en.wikipedia.org/wiki/ACID">ACID</a>
     * <a href="https://github.com/realm/realm-java/tree/master/examples">Examples using Realm</a>
     *
     */

部分源码分析:

public final class Realm extends BaseRealm {
//默认的文件名,是啥?
public static final String DEFAULT_REALM_NAME = RealmConfiguration.DEFAULT_REALM_NAME;
 
 
//怎么这么熟悉呢?是RxJava?
@Override
    @OptionalAPI(dependencies = {"rx.Observable"})
    public Observable<Realm> asObservable() {
        return configuration.getRxFactory().from(this);
    }
 
//下面这些方法,应该都能顾名思义吧
 
public <E extends RealmModel> void createAllFromJson(Class<E> clazz, JSONArray json) {
}
 
 
    public <E extends RealmModel> void createOrUpdateAllFromJson(Class<E> clazz, JSONArray json) {
}
 
 
    public <E extends RealmModel> E createOrUpdateObjectFromJson(Class<E> clazz, JSONObject json) {
}
 
 
    public <E extends RealmModel> E createObject(Class<E> clazz) {
}
 
 
    public <E extends RealmModel> E copyToRealmOrUpdate(E object) {
}
 
 
    public void executeTransaction(Transaction transaction) {
}
 
 
    public void delete(Class<? extends RealmModel> clazz) {
}
}

 RealmConfiguration类的说明翻译:

/**
 * 一个RealmConfiguration对象,可用来设置特定的Realm实例
 * RealmConfiguration实例只能通过io.realm.RealmConfiguration.Builder类的build()方法来创建
 * 想使用默认的RealmConfiguration实例,请使用io.realm.Realm#getDefaultInstance()方法。
 * 如果想使用自己配置RealmConfiguration实例的Realm实例,需要调用Realm#setDefaultConfiguration(RealmConfiguration)
 * 
 * <p>
 * 可以用下面代码创建一个最简单配置的实例:
 * RealmConfiguration config = new RealmConfiguration.Builder(getContext()).build())
 * 这样创建的实例,具有一下属性:
 * 
 * <ul>
 * <li>Realm的默认文件名是"default.realm"</li>
 * <li>"default.realm"文件保存在"Context.getFilesDir()"目录中</li>
 * <li>它的schema版本号设置为0</li>
 * </ul>
 */


部分源码分析:

public final class RealmConfiguration {
//默认文件名
public static final String DEFAULT_REALM_NAME = "default.realm";
 
//Rx工厂
private final RxObservableFactory rxObservableFactory;
 
//弱引用
private final WeakReference<Context> contextWeakRef;
 
/**
     * 
     * 从Asset目录中返回Realm文件名,还可以保存在Asset中?
     * @return input stream to the asset file.
     * @throws IOException if copying the file fails.
     */
    InputStream getAssetFile() throws IOException {
        Context context = contextWeakRef.get();
        if (context != null) {
            return context.getAssets().open(assetFilePath);
        } else {
        }
    }
 
/**
         * 使用app自己内置硬盘目录来存储Realm file。不需要任何扩展访问权限。
         * 默认目录为:/data/data/<packagename>/files,这个路径能否修改取决于供应商的具体实现
         * 
         * @param 参数context请使用application的context.
         */
        public Builder(Context context) {
            if (context == null) {
                throw new IllegalArgumentException("A non-null Context must be provided");
            }
            RealmCore.loadLibrary(context);
            initializeBuilder(context.getFilesDir());
        }

通过上面的翻译说明和源码分析,应该几乎明白了Realm的原理和基本使用了吧。总结下面几点:

  1. Realm保存的结果其实是在一个文件里面,默认的文件名是"default.realm",在"Context.getFilesDir()"目录中,即:/data/data/<packagename>/files/default.realm。意思是,当你在应用管理里面给当前app"清除数据",realm数据库的数据会丢失。故我们需要把默认的数据文件放到asset目录中,当数据库初始化时再copy到"Context.getFilesDir()"下。
  2. 在创建RealmConfiguration对象时,可以通过.assetFile(this,"realm file path in assets")方法指定初始化的数据库文件。Realm会把制定路径下的xxx.realm文件copy到Context.getFilesDir()目录中,以替换默认创建的空数据库文件。
  3. 可以设置默认文件名,通过RealmConfiguration类进行配置。路径似乎改不了,需要看具体设备供应商的实现。
  4. Realm的实例需要在每次的具体操作中获取,可以看成是一个数据操作的sessin,用完后必须close关闭。打开和关闭Realm实例,应当放在onCreate/onDestroy或者onStart/onStop方法中。
  5. Realm中似乎有RxJava的影子,支持链式异步任务?
  6. Realm中有个各种增删改差的方法,还可以根据JSON的数据实例化一个RealmObject子类java bean。
  7. 重点:切记,Realm数据库的主键字段不是自动增长的,需要自己设置,做添加的时候如果不给id字段值,默认会为0。后面再添加会报错,说id为0的数据已经存在。尤其是批量添加的时候要注意,当心出现只添加了一条记录的悲剧。
  8. 数据自动更新。mRealm.addChangeListener(this);//当数据库的数据有变化时,系统回调此方法。

经过上面的分析和总结,其实已经很明了了。还有些需要注意的地方,在代码中讲解。

application代码:

public class MyApplication extends Application {
    private String realmName = "dk.realm";
 
 
    @Override
    public void onCreate() {
        super.onCreate();
 
 
        RealmConfiguration realmConfig = new RealmConfiguration.Builder(this)
                .name(realmName)
                //.assetFile(this,"realm file path in assets,will copy this file to Context.getFilesDir() replace an empty realm file")
                .build();
        Realm.setDefaultConfiguration(realmConfig);
    }
}

java Bean:

public class TestUser extends RealmObject {
 
 
    @PrimaryKey
    private int userId;//id,主键
    @Required
    private String userName;//用户姓名,必填字段
    private String userPwd;//密码
    private int userAge;//年龄
    private String userAddress;//住址
    private String userWork;//工作
    private String userSex;//性别
 
 
    //private RealmList<E> list;  集合
//...
}

BaseDao,简单封装,把基本的增删改功能提取:

public class BaseDao {
    private Realm realm;
 
 
    public BaseDao(Realm realm) {
        this.realm = realm;
    }
 
 
    /**
     * 添加(性能优于下面的saveOrUpdate()方法)
     *
     * @param object
     * @return 保存或者修改是否成功
     */
    public boolean insert(RealmObject object) {
        try {
            realm.beginTransaction();
            realm.insert(object);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }
/**
     * 添加(性能优于下面的saveOrUpdateBatch()方法)
     *
     * @param list
     * @return 批量保存是否成功
     */
    public boolean insert(List<? extends RealmObject> list) {
        try {
            realm.beginTransaction();
            realm.insert(list);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }
//...
}

UserDao extends BaseDao:

/**
     * 单条保存demo
     */
    public boolean addOneTest() {
        boolean bl = false;
        try{
            realm.beginTransaction();
            //在数据库中创建一个对象,主键默认值为0
            TestUser user = realm.createObject(TestUser.class);//(类,主键)
 
 
            //更新数据库各自段的值
            user.setUserName("admin");
            //主键字段的值由0更新为55。而不是直接创建了一个id为55的对象
            user.setUserId(55);
            //...
            realm.commitTransaction();
            bl = true;
        }catch (Exception e){
            e.printStackTrace();
            realm.cancelTransaction();
        }
 
 
        /*try{
            realm.beginTransaction();
            TestUser user2 = new TestUser("hibrid", "120250", 26, "赣州", "贼", "男");
            //不给id,会被默认为0
            //user2.setUserId(102);
            TestUser userWithId = realm.copyToRealm(user2);
            realm.commitTransaction();
            bl = true;
        }catch (Exception e){
            e.printStackTrace();
            realm.cancelTransaction();
        }*/
        return bl;
    }
 
//init data
    public boolean init() {
        /**
         * 此处要注意,方法最后调用的是添加或者修改的方法。
         * 如果list的数据都不给id,则第一条记录添加成功后的id为0,后面的都在此基础上修改。
         * 最后的效果是,数据库只有一条记录,id为0,其他字段被更新为了最后一个对象的数据
         */
        List<TestUser> list = new ArrayList<>();
        list.add(new TestUser(0,"android", "123123", 20, "河南常德", "传菜员", "女"));
        list.add(new TestUser(1,"angel", "13588889988", 21, "云南西双版纳", "飞行员", "男"));
        list.add(new TestUser(2,"adidass", "110119", 28, "云南德克萨斯州", "海员", "男"));
        list.add(new TestUser(3,"hijack", "250250", 39, "加州电厂", "厨师", "女"));
        list.add(new TestUser(4,"hibrid", "120250", 26, "赣州", "贼", "男"));
        list.add(new TestUser(5,"admin", "123456", 20, "湖北汉城", "程序员", "女"));
        return saveOrUpdateBatch(list);
    }
 
/**
     * 条件查询
     *
     * @return 返回结果集合
     */
    public RealmResults<TestUser> findByAnyParams(HashMap<Object, Object> params) {
        //realm.where(TestUser.class)
        //可跟查询条件
        //.or()                      或者
        //.beginsWith()              以xxx开头
        //.endsWith()                以xxx结尾
        //.greaterThan()             大于
        //.greaterThanOrEqualTo()    大于或等于
        //.lessThan()                小于
        //.lessThanOrEqualTo()       小于或等于
        //.equalTo()                 等于
        //.notEqualTo()              不等于
        //.findAll()                 查询所有
        //.average()                 平均值
        //.beginGroup()              开始分组
        //.endGroup()                结束分组
        //.between()                 在a和b之间
        //.contains()                包含xxx
        //.count()                   统计数量
        //.distinct()                去除重复
        //.findFirst()               返回结果集的第一行记录
        //.isNotEmpty()              非空串
        //.isEmpty()                 为空串
        //.isNotNull()               非空对象
        //.isNull()                  为空对象
        //.max()                     最大值
        //.maximumDate()             最大日期
        //.min()                     最小值
        //.minimumDate()             最小日期
        //.sum()                     求和
        return realm.where(TestUser.class).findAll();
    }


MainActivity代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mRealm = Realm.getDefaultInstance();
        userDao = new UserDao(mRealm);
 
 
        //...
 
 
        /**
         * 数据库数据更新监听
         */
        mRealm.addChangeListener(this);
    }
 
//...
 
 
@Override
    public void onChange(Realm element) {
        findAll();
    }
 
@Override
    protected void onDestroy() {
        userDao = null;
        mRealm.close();
        super.onDestroy();
    }

增删改的代码注意事务,其他的都简单。
 


Android Realm数据库使用总结及采坑记录

Realm使用注意事项

  • Realm默认运行在主线程,使用时须开启异步任务
  • Realm本身是单例类,可以多线程并发调用,但是RealmObject则不允许并发,每个RealmObject都绑定了一个TreadId,必须在创建该RealmObject的线程中使用它.
  • 在子线程查询出的数据无法在主线程使用,自己的方案是:子线程查询,置换为自己的Bean类,然后在主线程使用
  • 没有主键的realmObject无法进行update操作.所以如果要使用realm.copyToRealmOrUpdate(realmObject),那么这个realmObject必须设置primaryKey
  • 如果Realm关闭,所有查询得到的RealmObject都不能使用了,解决方案是复制一份数据到内存中。
  • 操作数据库必须在transaction中完成

常见问题

Object not managed by Realm, so it cannot be removed

Realm不支持直接通过deleteFromRealm删除Bean类,即使该Bean extends RealmObject,否则会报此异常

正确姿势:

根据指定字段,从数据库中查询到该Bean,然后再删除

/**
 * 从数据库中删除CollectBean
 * @param conType
 * @param relateId
 */
public void deleteCollectBeanByTypeAndId(String conType,int relateId){
    Realm realm = RealmUtils.getInstance().mRealm;
    CollectBean bean = realm.where(CollectBean.class)
            .equalTo(CollectBean.CON_TYPE, conType)
            .equalTo(CollectBean.RELATE_ID,relateId)
            .findFirst();
    realm.beginTransaction();
    bean.deleteFromRealm();
    realm.commitTransaction();
}

Realm accessed from incorrect thread

RealmObject自带线程保护功能,只能在创建它的线程中访问,在子线程中不能访问。
也就是说,如果你在主线程中new了一个RealmObject对象 user,那么在子线程中是访问不了user对象的。
要想在子线程中访问,必须先将user存入Ream中,然后在子线程中query出来。

更多介绍:Realm的常规使用与线程中的坑 - 简书

is not part of the schema for this Realm

详细异常信息: java.lang.IllegalArgumentException: UserBean is not part of the schema for this Realm

需要调整plugin中的顺序,如下:

apply plugin: 'com.android.application'
apply plugin: 'com.bugtags.library.plugin'
apply plugin: 'android-apt'
apply plugin: 'realm-android'
apply plugin: 'com.neenbedankt.android-apt'


{bean}has a primary key, use ‘createObject(Class, Object)’ instead

详细异常信息: io.realm.exceptions.RealmException: ‘UserBean’ has a primary key, use ‘createObject(Class, Object)’ instead.

如果实体中已经通过@PrimaryKey标明了主键,那么想要通过createObject(Class<E>, Object)创建实体对象,则必须传入primaryKeyValue(主键值)

异步查询之坑
1.官方文档介绍 主线程操作Realm会卡顿/阻塞线程

官方表示Realm运行速度很快,足以在主线程运行,而后又表示其实还是会阻塞线程导致偶发的ANR,因此建议在子线程操作Realm.

2.子线程查询的数据,无法在主线程使用

解决方案:

子线程查询,置换为自己的Bean类,然后在主线程使用.

Realm.getDefaultInstance().executeTransactionAsync(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        Person ziPerson = realm.where(Person.class).findFirst();
        personInfo = new PersonInfo();
        personInfo.setName(ziPerson.getName());
        personInfo.setAge(ziPerson.getAge());
        //Log 输出#Execute ] false..Person{name='小明', age=18}
        KLog.i((Looper.getMainLooper()==Looper.myLooper())+".."+ personInfo.toString());
    }
}, new Realm.Transaction.OnSuccess() {
    @Override
    public void onSuccess() {
        //Log 输出#OnSuccess ] true..personInfo:Person{name='小明', age=18}
        KLog.i((Looper.getMainLooper()==Looper.myLooper())+".."+ personInfo.toString());
    }
}, new Realm.Transaction.OnError() {
    @Override
    public void onError(Throwable error) {
        KLog.i(error.toString());
    }
});
RejectedExecutionException

详细异常信息:java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@4dffbdd rejected from io.realm.internal.async.RealmThreadPoolExecutor@c09c352[Running, pool size = 17, active threads = 2, queued tasks = 100, completed tasks = 110]

解决方案:

不要在for循环中使用Realm,将数据存入集合中,然后开启事务,直接使用copyToRealmOrUpdate(realmObjectList)存储即可.

事务嵌套报异常

详细异常信息:The Realm is already in a write transaction in /Users/blakemeike/Working/release/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 116

原因 : 在一个事务中开启了另外一个事务.应避免这种情况.
 


对Android Realm数据库进行加密及版本升级管理

Realm从设计之初便是为适应移动端的使用场景。使用简洁,操作速度快。是一款很不错的移动端嵌入式数据库。
 

public class RealmDBConfig {
    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
    static RealmConfiguration realmConfig = null;
    private static int version = 1;  // 数据库版本号
    /**
     * 初始化数据库
     */
    public static void setInitRealm(int version) {
        RealmDBConfig.version= version;
        if (Realm.getDefaultConfiguration() == null) {
            Realm.init(MyApplication.getAppContext());
        }
        configRealm(version); 
    }
 
    private static void configRealm(int version) {
        ECKey ecKey = ECKey.fromPrivate(OcMath.toBigInt("0abc4301"));
        byte[] sha256 = Sha256Hash.hash(ecKey.getPubKey());
        realmConfig = new RealmConfiguration.Builder()
                //设置数据库升级
                .migration(migrationDb)
                //设置数据库密码
                .encryptionKey(OcMath.toHexStringNoPrefix(sha256).getBytes())  
                .name("test.realm")
                //是否允许在UI线程中操作写入数据库
                .allowWritesOnUiThread(true)
                //设置数据库版本
                .schemaVersion(version)
                .build();
        Realm.setDefaultConfiguration(realmConfig);
    }
 
    public static Realm getRealm() {
        return Realm.getInstance(getRealmConfiguration());
    }
 
    public static RealmConfiguration getRealmConfiguration() {
        if (realmConfig == null) {
            setWalletOCRealm();
        }
        return realmConfig;
    }
 
 
    /**
     * 数据库版本管理
     */
    protected static RealmMigration migrationDb = new RealmMigration() {//升级数据库
        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            RealmSchema schema = realm.getSchema();
            //给已有表新增字段
            if (oldVersion == 1) {
                schema.get("Rm_User").addField("nickname",String.class);
              
            }
            //新增表
            if (oldVersion == 2) {
                schema.create("Rm_Order")
                        .addField("orderId", String.class)
                        .addField("name", String.class)
                        .addField("number", int.class);
            }
            //删除字段
            if (oldVersion == 3) {
                schema.get("Rm_Wallet")
                        .removeField("hash", String.class);
            }
            //删除表
            if (oldVersion == 4) {
                schema.remove("Rm_Contract");
            }
            oldVersion++;
        }
    };
 
}

Realm 升级,更换主键,更换字段类型

该篇文章主要讲解在android上使用Realm,关于升级的文章!

1.新增一个表(或者说新增一个类让其成为数据表)
2.更换已经存在的表中的字段类型(例如Int 类型更换为String类型)
3.更换已经存在的表中的主键

升级数据库很简单,在调用Realm实例的时候配置config时传入我们自己写好的MyMigration类即可,当然数据库的version也需要增加

object RealmHelper {
    private fun getRealmConfig(): RealmConfiguration {
        return RealmConfiguration.Builder()
                .name(RealmConstant.DB_NAME)
                .schemaVersion(RealmConstant.DB_VERSION)
                .migration(MyMigration())
                .build()
    }
 
    @JvmStatic
    fun getRealmInstance():Realm{
        return Realm.getInstance(getRealmConfig())
    }
}

接下来你要实现上述说的1,2,3只要在MyMigration类中实现即可

1.新增一个表

Realm数据库和传统SQL数据库增加表不一样,Realm只要增加一个表就要升级!新增表的类
例如我第一版本的DB_VERSION=0,现在我要新增一个表
a.DB_VERSION=1
b.实现新增的表类
c.在MyMigration处理升级

open class RedBookChapter(@PrimaryKey var chapterId: Int = -1, @Index var bookId: String = "") : RealmObject()
class MyMigration : RealmMigration {
    override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
        var oldV = oldVersion
        val schema = realm.schema
        if (oldV == 0L) {
            val bookChapterSchema = schema.create("RedBookChapter")
            bookChapterSchema?.let {
                it.addField("chapterId", Int::class.java, FieldAttribute.PRIMARY_KEY)
                        .addField("bookId", String::class.java, FieldAttribute.INDEXED)
                        .setRequired("bookId", true)
                
            }
            oldV++
        }
    }
 
 
    /**
     *  java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file.
     *  The most likely cause is that equals() and hashCode() are not overridden in the migration class:
     *  com.apusapps.reader.provider.realm.MyMigration
     */
    override fun hashCode(): Int {
        return MyMigration::class.java.hashCode()
    }
 
    override fun equals(other: Any?): Boolean {
        if (other == null) {
            return false
        }
        return other is MyMigration
    }
}

这样在每次操作数据库时,自然检查版本号就处理升级了!

2.更换已经存在的表中的字段的类型(例如Int 类型更换为String类型)

新增表中的字段很简单,addField即可,但是这里要说的是更换表中已经存在的字段的类型,并且字段名不变(即只更换字段的类型)

例如我第上一版本的DB_VERSION=1,现在我要更换字段的类型
a.DB_VERSION=2
b.处理更换字段的类
c.在MyMigration处理升级

原来的类

open class BookColl(@PrimaryKey var bookId: String = "",
                    var briefIntro: String? = "",
                    var majorCateId: Int? = -1,
                    var majorCateName: String? = "",
                    var minorCateId: Int? = -1,
                    var minorCateName: String? = ""
) : RealmObject()


修改后的类

open class BookColl(@PrimaryKey var bookId: String = "",
                    var briefIntro: String? = "",
                    var majorCateId: String? = "",
                    var majorCateName: String? = "",
                    var minorCateId: String? = "",
                    var minorCateName: String? = ""
) : RealmObject()
class MyMigration : RealmMigration {
    override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
        var oldV = oldVersion
        val schema = realm.schema
        if (oldV == 0L) {
            val bookChapterSchema = schema.create("RedBookChapter")
            bookChapterSchema?.let {
                it.addField("chapterId", Int::class.java, FieldAttribute.PRIMARY_KEY)
                        .addField("bookId", String::class.java, FieldAttribute.INDEXED)
                        .setRequired("bookId", true)
                
            }
            oldV++
        }
 
        if (oldV == 1L) {
            val bookCollSchema = schema.get("BookColl")
            bookCollSchema?.let {
                it.addField("majorCateId_temp", String::class.java)
                        .addField("minorCateId_temp", String::class.java)
                        .transform { obj ->
                            obj.setString("majorCateId_temp", obj.getInt("majorCateId").toString())
                            obj.setString("minorCateId_temp", obj.getInt("minorCateId").toString())
                        }
                        .removeField("majorCateId")
                        .removeField("minorCateId")
                        .renameField("majorCateId_temp", "majorCateId")
                        .renameField("minorCateId_temp", "minorCateId")
                
            }
            oldV++
        }
    }
 
 
    /**
     *  这里的hashCode,和equals同上面一样,这里省略
     */
   
}

说明下:

1.命名临时的字段majorCateId_temp,minorCateId_temp
2.将DB_VERSION=1中老用户的majorCateId和minorCateId这些字段迁移到DB_VERSION=2中的临时字段是上
3.移除老的字段
4.重新命名,将majorCateId_temp等字段改为之前的字段

3.更换已经存在的表中的主键

因为DB_VERSION=1时,新增了RedBookChapter,但是我的主键用错了,需要重新更换主键。
因为中间已经有了DB_VERSION=2了,所以这里DB_VERSION=3

a.DB_VERSION=3
b.处理更换主键的类
c.在MyMigration处理升级

更换主键之前的类

open class RedBookChapter(@PrimaryKey var chapterId: Int = -1, @Index var bookId: String = "") : RealmObject()


更换主键后的类

open class RedBookChapter(@PrimaryKey var hashCode:String = "",
                             var chapterId: Int = -1, @Index var bookId: String = "") : RealmObject()


注意这里的hasCode不是真正的hashCode,是我用bookId和chapterId拼装的:bookId.plus(chapterId.toInt()),要知道一个类的hashCode会随时变的。

class MyMigration : RealmMigration {
    override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
        var oldV = oldVersion
        val schema = realm.schema
        if (oldV == 0L) {
            val bookChapterSchema = schema.create("RedBookChapter")
            bookChapterSchema?.let {
                it.addField("chapterId", Int::class.java, FieldAttribute.PRIMARY_KEY)
                        .addField("bookId", String::class.java, FieldAttribute.INDEXED)
                        .setRequired("bookId", true)
                
            }
            oldV++
        }
 
        if (oldV == 1L) {
            val bookCollSchema = schema.get("BookColl")
            bookCollSchema?.let {
                it.addField("majorCateId_temp", String::class.java)
                        .addField("minorCateId_temp", String::class.java)
                        .transform { obj ->
                            obj.setString("majorCateId_temp", obj.getInt("majorCateId").toString())
                            obj.setString("minorCateId_temp", obj.getInt("minorCateId").toString())
                        }
                        .removeField("majorCateId")
                        .removeField("minorCateId")
                        .renameField("majorCateId_temp", "majorCateId")
                        .renameField("minorCateId_temp", "minorCateId")
                
            }
            oldV++
        }
 
        if (oldV == 2L) {
            val bookRedChapter = schema.get("RedBookChapter")
            bookRedChapter ?.let {
                it.addField("hashCode", String::class.java)
                        .addField("chapterId_temp",Int::class.java)
                        .transform { obj ->
                            obj.setString("hashCode", obj.getString("bookId").plus(obj.getInt("chapterId")))
                            obj.setInt("chapterId_temp", obj.getInt("chapterId"))
                        }
                        .removeField("chapterId")
                        .renameField("chapterId_temp","chapterId")
                        .addPrimaryKey("hashCode")
                        .setRequired("hashCode",true)
                
            }
            oldV++
        }
    }
 
    /**
     *  这里的hashCode,和equals同上面一样,这里省略
     */
}

到这里文章开头说的1,2,3要完成的事已经处理完毕了。
其实只要处理好自己原来的类,处理好MyMigration即可。
 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio提供了多种数据库选项,其中包括SQLite、Realm和Room等。其中,SQLite是默认的本地数据库选项,而Realm和Room则提供更高级别的功能。 以下是使用SQLite在Android Studio中创建和使用数据库的基本步骤: 1. 创建一个新的Android Studio项目,并在build.gradle文件中添加以下依赖项: ``` dependencies { implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:support-core-utils:28.0.0' implementation 'com.android.support:support-annotations:28.0.0' implementation 'com.google.android.gms:play-services-maps:15.0.1' // SQLite implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' } ``` 2. 在app的build.gradle文件中添加以下配置: ``` defaultConfig { applicationId "com.example.myapp" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" // 添加数据库的名称和版本号 javaCompileOptions { annotationProcessorOptions { arguments = ["room.schemaLocation": "$projectDir/schemas".toString()] } } ndk { abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64' } } // 定义数据库的版本号 def ROOM_VERSION = "2.2.5" // 添加Room的依赖项 dependencies { // Room components implementation "androidx.room:room-runtime:$ROOM_VERSION" annotationProcessor "androidx.room:room-compiler:$ROOM_VERSION" // Optional RxJava2 support for Room implementation "androidx.room:room-rxjava2:$ROOM_VERSION" // Optional Guava support for Room, including Optional and ListenableFuture implementation "androidx.room:room-guava:$ROOM_VERSION" // Test helpers testImplementation "androidx.room:room-testing:$ROOM_VERSION" } ``` 3. 创建一个DatabaseHelper类,用于创建和管理数据库: ``` public class DatabaseHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "myapp.db"; private static final String TABLE_NAME = "mytable"; private static final String COLUMN_ID = "id"; private static final String COLUMN_NAME = "name"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME + " TEXT" + ")"; db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public void addData(String name) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, name); db.insert(TABLE_NAME, null, values); db.close(); } public List<String> getAllData() { List<String> data = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null); if (cursor.moveToFirst()) { do { data.add(cursor.getString(1)); } while (cursor.moveToNext()); } cursor.close(); db.close(); return data; } } ``` 4. 在Activity中使用DatabaseHelper类来添加和获取数据: ``` public class MainActivity extends AppCompatActivity { private DatabaseHelper db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = new DatabaseHelper(this); db.addData("John"); db.addData("Jane"); List<String> data = db.getAllData(); for (String name : data) { Log.d("MainActivity", "Name: " + name); } } } ``` 这样就可以在Android Studio中使用SQLite数据库了。当然,如果需要更高级别的功能,可以考虑使用Realm或Room。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值