Realm 是什么? 用来干什么的?为什么要使用它?使用的优缺点?不用它有什么替换方案?
跟Sqlite、GreenDao等的区别,优缺点。
简介是什么鬼?不说。详情请点击:https://realm.io/docs/java/latest/
1) 在项目的build文件加上
repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath "io.realm:realm-gradle-plugin:5.1.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
2) 在app的build文件加上
apply plugin: 'realm-android'
1)在Application的oncreate()方法中Realm.init();
public class LocationApplication extends MultiDexApplication { private static LocationApplication mLocationApplication; public static LocationApplication getInstance () { return mLocationApplication; } @Override public void onCreate () { super.onCreate(); mLocationApplication = this; Realm.init( this ); //可以在此进行配置 // RealmConfiguration config = new RealmConfiguration.Builder() // .name( "study.realm" ) // .schemaVersion( 0 ) // .build(); // Realm.setDefaultConfiguration( config ); } }2) 在app的build文件加上
<application android:name=".LocationApplication" ... </application>
1)查询数据
使用事物操作
//1.使用executeTransaction进行操作 mRealm.executeTransaction( new Realm.Transaction() { @Override public void execute ( Realm realm ) { RealmResults< CmlUser > cl = mRealm.where( CmlUser.class ).findAll(); for ( CmlUser cmlUser : mDBUtil.getCmlUserAll() ) { Log.e( "全部数据", cmlUser.toString() ); } } } );
或使用事物块
// 2.使用 // mRealm.beginTransaction(); // ...处理... // mRealm.commitTransaction(); mRealm.beginTransaction(); RealmResults< CmlUser > cl = mRealm.where( CmlUser.class ).findAll(); for ( CmlUser cmlUser : cl ) { Log.e( "全部数据", cmlUser.toString() ); } mRealm.commitTransaction();
2)删除数据(使用事物操作或事物块,以下四中只使用事物块)
单条数据
mRealm.executeTransaction( new Realm.Transaction() { @Override public void execute ( Realm realm ) { CmlUser mCmlUser = realm.where( CmlUser.class ).findFirst(); mCmlUser.deleteFromRealm(); } } );
全部数据
mRealm.executeTransaction( new Realm.Transaction() { @Override public void execute ( Realm realm ) { RealmResults< CmlUser > cl = mRealm.where( CmlUser.class ).findAll(); cl.deleteAllFromRealm(); } } );
3)新增数据
mRealm.executeTransaction( new Realm.Transaction() { @Override public void execute ( Realm realm ) { // 有主键时使用 // realm.copyToRealmOrUpdate( mCmlUser );//java.lang.IllegalArgumentException: A RealmObject with no @PrimaryKey cannot be updated: class com.dianxun.study.realm.CmlUser realm.copyToRealm( mCmlUser ); } } );
3)update数据
mRealm.executeTransaction( new Realm.Transaction() { @Override public void execute ( Realm realm ) { CmlUser mCmlUser = realm.where( CmlUser.class ).findFirst(); mCmlUser.setName( "修改后测试2" ); mCmlUser.setDemo( "测试完成" ); // realm.copyToRealmOrUpdate( mCmlUser );//有主键时使用 realm.copyToRealm( mCmlUser ); } } );
官方版本请看官网,或查看RaphetS同学的https://www.jianshu.com/p/28912c2f31db
以下是自我构筑物-造轮子
public class CmlUser extends RealmObject { @PrimaryKey private int id; @Required private String name; private String url; private String pic; private int age; private String demo; get/set()... }
以下为自定义的DBUtil--如果写出来有什么问题,请联系我!
//数据库名称 private static final String dbName = "study.realm"; //当前数据库版本 private static final int dbVersion = 1; private static DBUtil mDbUtil; private Realm mRealm; private DBUtil () { RealmMigration realmMigration = new RealmMigration() { @Override public void migrate ( @NonNull DynamicRealm realm, long oldVersion, long newVersion ) { if ( oldVersion < newVersion ) { //更新数据库结构(比如增加字段) updateFields( realm ); } } }; RealmConfiguration config = new RealmConfiguration.Builder() //设置数据库名称 .name( dbName ) //设置数据库版本 .schemaVersion( dbVersion ) //版本变化时(迁移操作),怎么处理。。。 .migration( realmMigration ) //版本冲突时自动删除原数据库 // .deleteRealmIfMigrationNeeded() .build(); try { //手动调用Realm.migrateRealm(),不手动调起此方法,使用时才会调起RealmMigration Realm.migrateRealm( config, realmMigration ); } catch ( FileNotFoundException e ) { e.printStackTrace(); } mRealm = Realm.getInstance( config ); }
/** * 获取实例 * * @return DBUtil实例 */ public static DBUtil getInstance () { if ( mDbUtil == null ) { mDbUtil = new DBUtil(); } return mDbUtil; }
/** * 获取手机存储用户信息 * * @return 第一个 */ public CmlUser getCmlUserFirst () { mRealm.beginTransaction(); CmlUser mCmlUser = mRealm.where( CmlUser.class ).findFirst(); mRealm.commitTransaction(); if ( null == mCmlUser ) { return null; } return mCmlUser; }
/** * 查询全部存储的数据 */ public RealmResults< CmlUser > getCmlUserAll () { mRealm.beginTransaction(); RealmResults< CmlUser > mCmlUser = mRealm.where( CmlUser.class ).findAll(); mRealm.commitTransaction(); if ( StringUtil.isNullOrEmpty( mCmlUser ) && mCmlUser.size() == 0 ) { return null; } return mCmlUser; }
/** * 保存用户信息 * * @param mCmlUser 实体类 */ public void saveUser ( CmlUser mCmlUser ) { if ( getCmlUser( mCmlUser.getName() ) != null ) { mRealm.beginTransaction(); mRealm.insertOrUpdate( mCmlUser ); mRealm.commitTransaction(); return; } mRealm.beginTransaction(); mRealm.copyToRealmOrUpdate( mCmlUser ); mRealm.commitTransaction(); }
/** * 删除数据--用户信息 */ public void delete () { mRealm.beginTransaction(); CmlUser mCmlUser = mRealm.where( CmlUser.class ).findFirst(); if ( mCmlUser == null ) { mRealm.commitTransaction(); return; } mCmlUser.deleteFromRealm(); mRealm.commitTransaction(); }
使用时:
mCmlUser = mDBUtil.getCmlUserFirst();
mDBUtil.saveUser( mCmlUser );
mDBUtil.delete();版本更新时:以下为增加字段或减字段时
增加字段:
//加字段 for ( Field changeField : changeFields ) { String changeName = changeField.getName(); if ( filedMap.containsKey( changeName ) ) { //假如字段名已存在就进行下次循环 Log.e( "数据库字段", changeName + "该字段已存在" ); } else { //不存在,就放到map中,并且给表添加字段 filedMap.put( changeName, changeName ); Log.e( "数据库字段", changeName + "该字段已添加到表中" ); //根据属性的类型给表增加字段 String type = changeField.getType().toString(); if ( type.equals( "class java.lang.String" ) ) { personSchema.addField( changeName, String.class, FieldAttribute.REQUIRED ); continue; } if ( type.equals( "int" ) ) { personSchema.addField( changeName, int.class, FieldAttribute.REQUIRED ); continue; } if ( type.equals( "class java.lang.Long" ) ) { personSchema.addField( changeName, long.class, FieldAttribute.REQUIRED ); continue; } if ( type.equals( "boolean" ) ) { personSchema.addField( changeName, Boolean.class, FieldAttribute.REQUIRED ); } else { Log.i( "数据库增加字段失败", "数据库出错" ); } } }
删除字段:
//减字段 Map< String, String > changeFieldMap = new HashMap<>(); for ( Field changeField : changeFields ) { String changeName = changeField.getName(); //得到更改后表的字段 changeFieldMap.put( changeName, changeName ); } for ( String field : filedMap.keySet() ) { if ( changeFieldMap.containsKey( field ) ) { Log.e( "数据库字段", field + "该字段还存在于表中" ); } else { Log.e( "数据库字段", field + "该字段已移除" ); personSchema.removeField( field ); } }
在此,多谢各位浏览!~!~