diff --git a/README.md b/README.md index cdffdaa..c1707cd 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ The original code is from [stackoverflow](http://stackoverflow.com/a/30334668/71 ``` dependencies { compile 'org.greenrobot:greendao:3.2.0' - compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0' + compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.4.0' } ``` or (greendao 3.0 below) @@ -47,7 +47,7 @@ public class MySQLiteOpenHelper extends DaoMaster.OpenHelper { super(context, name, factory); } @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + public void onUpgrade(Database db, int oldVersion, int newVersion) { MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class); } } diff --git a/README_CH.md b/README_CH.md index bc86ada..65fa7be 100644 --- a/README_CH.md +++ b/README_CH.md @@ -23,7 +23,7 @@ GreenDaoUpgradeHelper是一个greenDao的数据库升级帮助类。使用它可 ``` dependencies { compile 'org.greenrobot:greendao:3.2.0' - compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0' + compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.4.0' } ``` 如果你使用的greendao是3.0以前的版本,请使用下面的依赖: @@ -45,7 +45,7 @@ public class MySQLiteOpenHelper extends DaoMaster.OpenHelper { super(context, name, factory); } @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + public void onUpgrade(Database db, int oldVersion, int newVersion) { MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class); } } diff --git a/app/build.gradle b/app/build.gradle index eb6486f..68566c9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,4 +32,5 @@ dependencies { // compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.1.0' compile project(':library') + compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar' } diff --git a/app/src/main/java/com/github/yuweiguocn/demo/greendao/MainActivity.java b/app/src/main/java/com/github/yuweiguocn/demo/greendao/MainActivity.java index 895e019..bb77d0d 100644 --- a/app/src/main/java/com/github/yuweiguocn/demo/greendao/MainActivity.java +++ b/app/src/main/java/com/github/yuweiguocn/demo/greendao/MainActivity.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.os.Bundle; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -11,8 +12,12 @@ import com.github.yuweiguocn.demo.greendao.base.BaseActivity; import com.github.yuweiguocn.demo.greendao.db.DaoMaster; import com.github.yuweiguocn.demo.greendao.db.MySQLiteOpenHelper; +import com.github.yuweiguocn.demo.greendao.db.TestData2; +import com.github.yuweiguocn.demo.greendao.db.TestData2Dao; import com.github.yuweiguocn.library.greendao.MigrationHelper; +import java.util.Date; + public class MainActivity extends BaseActivity { private DaoMaster daoMaster; @@ -27,8 +32,12 @@ protected void onCreate(Bundle savedInstanceState) { MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "test.db", null); - daoMaster = new DaoMaster(helper.getWritableDatabase()); + daoMaster = new DaoMaster(helper.getEncryptedWritableDb("12342")); + TestData2 testData2 = new TestData2(0L, "12342", 123L, new Date(), 1234, true); + TestData2Dao testData2Dao = daoMaster.newSession().getTestData2Dao(); + testData2Dao.insert(testData2); + Log.d("MigrationHelper", "TestData2 " + testData2Dao.loadAll().toString()); } diff --git a/app/src/main/java/com/github/yuweiguocn/demo/greendao/db/MySQLiteOpenHelper.java b/app/src/main/java/com/github/yuweiguocn/demo/greendao/db/MySQLiteOpenHelper.java index a2f890f..b8c6550 100644 --- a/app/src/main/java/com/github/yuweiguocn/demo/greendao/db/MySQLiteOpenHelper.java +++ b/app/src/main/java/com/github/yuweiguocn/demo/greendao/db/MySQLiteOpenHelper.java @@ -5,6 +5,8 @@ import com.github.yuweiguocn.library.greendao.MigrationHelper; +import org.greenrobot.greendao.database.Database; + /** * Created by Growth on 2016/3/3. */ @@ -12,8 +14,9 @@ public class MySQLiteOpenHelper extends DaoMaster.OpenHelper { public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) { super(context, name, factory); } + @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + public void onUpgrade(Database db, int oldVersion, int newVersion) { MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class); } } diff --git a/library/src/main/java/com/github/yuweiguocn/library/greendao/MigrationHelper.java b/library/src/main/java/com/github/yuweiguocn/library/greendao/MigrationHelper.java index 6ee4983..49088aa 100644 --- a/library/src/main/java/com/github/yuweiguocn/library/greendao/MigrationHelper.java +++ b/library/src/main/java/com/github/yuweiguocn/library/greendao/MigrationHelper.java @@ -20,7 +20,7 @@ /** * - * please call {@link #migrate(SQLiteDatabase, Class[])} + * please call {@link #migrate(SQLiteDatabase, Class[])} or {@link #migrate(Database, Class[])} * */ public final class MigrationHelper { @@ -31,9 +31,12 @@ public final class MigrationHelper { private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master"; public static void migrate(SQLiteDatabase db, Class>... daoClasses) { + printLog("【The Old Database Version】" + db.getVersion()); Database database = new StandardDatabase(db); + migrate(database, daoClasses); + } - printLog("【The Old Database Version】" + db.getVersion()); + public static void migrate(Database database, Class>... daoClasses) { printLog("【Generate temp table】start"); generateTempTables(database, daoClasses); printLog("【Generate temp table】complete");