GreenDao和LitePal的一些使用心得

这里有个大神有更加详细


总的来讲,两者都是比较好用的工具,虽然都无法满足我的一个需求:自动存储float[]数组。

但是GreenDao在编译的过程中就会告诉你无法解析float[]数组类型,但是LitePal在使用过程中直接抛出异常(无法找到与之匹配的类型)。

GreenDao报错


LitePal使用过程中报错(不是我使用方法不当,我试过不加float[]类型的就不会报错)

GreenDao和LitePal的一些使用心得_第1张图片

其次,我发现,LitePal在创建表的时候会把float[]数据类型当成text类型去存储,这也刚好让我知道为啥无法取出来float[]数组的原因了。

当然,如果你没有这种存储特殊类型的需求,可以忽略我上面说的。


两者的使用,我只是简单的使用了一下,个人觉得还是LitePal方便使用,GreenDao结构更加清晰。

比如

加载jar包以及一些配置

GreenDao:

// In your root build.gradle file:
buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
}
 
// In your app projects build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
 
dependencies {
    compile 'org.greenrobot:greendao:3.2.2' // add library
}

LitePal:

(1)首先需要导入jar包

(2)在arrest文件夹下创建一个xml配置文件



       //数据库名称
       //数据库版本号

    
        //对应的Model
        //对应的Model,如果还有表直接在标签下添加即可
    

(3)在AndroidManifest.xml 中修改Application节点的名称


    
        ...
    


初始化

GreenDao:

devOpenHelper = new DaoMaster.DevOpenHelper(MainActivity.this,"person.db");
daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
 daoSession = daoMaster.newSession();

LitePal:

LitePal.getDatabase();

增加数据

GreenDao

PersonDao personDao = daoSession.getPersonDao();
Person person = new Person("HHH",12);
personDao.insert(person);

LitePal

Animal animal = new Animal("SSS",12);
        animal.save();

查询数据

GreenDao

PersonDao personDao = daoSession.getPersonDao();
        List persons = personDao.queryBuilder().build().list();
        for(Person person : persons){
            Log.i("Magic",""+person.toString());
        }

LitePal

 List  users = DataSupport.findAll(User.class);
        for(User user2 : users){
            Log.i("Magic",""+user2.getUserName());
        }

其他功能留给你们慢慢发现吧,目前为止,个人觉得还是LitePal好用一点(不考虑速度什么的)


你可能感兴趣的:(android)