使用realm替代sqlite

domain

public class Known extends RealmObject{

    @PrimaryKey
    private String id;

    private String title;

    private String content;

    private Date createTime;

    private String tag;

    public Known() {
    }

    public Known(String id,String title,String content,Date createTime) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.createTime = createTime;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

初始化配置

public class AppContext extends Application{

    private static AppContext applicationContext;

    private static RealmConfiguration realmConfiguration;

    public static AppContext getInstance(){
        return applicationContext;
    }

    public static boolean selectDrawer = false;

    public static Date selectDate = null;

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        realmConfiguration = new RealmConfiguration
                .Builder(this)
                .name("known")//如果你想設置個別資料庫名稱
//                .encryptionKey(key)//如果你需要加密的話
                .build();
    }

    public static RealmConfiguration getRealmConfig(){
        return realmConfiguration;
    }
}

初始化和关闭realm

public class MainActivity extends AppCompatActivity {

private Realm realm;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        realm = Realm.getInstance(AppContext.getRealmConfig());
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }

}

使用

public List<Known> findAll(Realm realm){
        return realm.where(Known.class).findAll();
    }

    public void insert(Known known,Realm realm){
        known.setId(UUID.randomUUID().toString());
        realm.beginTransaction();
        realm.copyToRealm(known);
        realm.commitTransaction();
    }

    public void update(final Known known,Realm realm){
        realm.beginTransaction();
        realm.copyToRealmOrUpdate(known);
        realm.commitTransaction();
    }

    public void delete(String id,Realm realm){
        RealmResults<Known> results = realm.where(Known.class).equalTo("id",id).findAll();
        realm.beginTransaction();
        results.clear();
        realm.commitTransaction();
    }

相关文章

安装 在Windows上安装SQLite。 访问官网下载下Precompliled Binaries for Windows的两个压缩包。 创建s...
一、安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries for Windows下的包下...
实例: 会员信息管理 功能:1.查看数据库 2.清空数据库 3.增加会员 4.删除会员 5.更新会员 6.查找会员  ...
关于SQLite SQLite是一个轻量的、跨平台的、开源的数据库引擎,它的在读写效率、消耗总量、延迟时间和整...