Android数据库框架——GreenDao3.2的配置 升级

网上有很多教程 官网上也有,所以我这边大概贴一下配置代码


apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            debuggable true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}
apply plugin: 'org.greenrobot.greendao'
// 需要配置的地方 -----2    GreenDao
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'
    // 需要配置的地方 -----1    GreenDao
    compile 'org.greenrobot:greendao:3.2.0'//GreenDao
    compile'org.greenrobot:greendao-generator:3.0.0'
//    compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0'
}
// 需要配置的地方 -----3    GreenDao
greendao{
    schemaVersion 1 //版本号,升级时可配置
    targetGenDir 'src/main/java'//生成目录
    daoPackage 'com.frontsurft.greendao.gen' //包名
}

一些封装的方便的类 网上也有。可以参考 :http://blog.csdn.net/qq_26787115/article/details/51811984

还有说到数据库升级的时候 更改版本号 schemaVersion 为2  DaoMaster.onUpgrade 的代码如下

/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
    public DevOpenHelper(Context context, String name) {
        super(context, name);
    }

    public DevOpenHelper(Context context, String name, CursorFactory factory) {
        super(context, name, factory);
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
        dropAllTables(db, true);
        onCreate(db);
    }
}
数据库升级
前面,我们学会了greenDAO的使用,下面来学习下升级。
今天研究了下升级,掌握方法了还是蛮简单的,这里对数据库的升级,仅仅是添加字段,添加表。对于删除,修改字段这里不多讲,因为sqlite数据库不适合此操作:
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.

其实我发现  要支持增加表 增加字段   比如增加字段  直接在bean里增加  增加表也是按照前面步骤 然后 在点击make project  然后升级版本号 即可 


参考  http://www.jianshu.com/p/a7a9336187b1

点击打开链接

猜你喜欢

转载自blog.csdn.net/w6718189/article/details/58601294