以前在開發過程當中,數據庫基本上會使用Litepal或者SQlite本身寫,最近換新環境,公司原先使用的數據庫就是GreenDao,在各類狀況的做用下,準備瞭解下GreenDao,順便寫一篇文章記錄下GreenDao的基本使用!你們在使用GreenDao的時候遇到什麼問題,歡迎在個人公衆號aserbao給我留言,無償服務!同時,歡迎你們來加入微信羣二維碼討論羣,一塊兒討論Android開發技術!羣二維碼定時在我公衆號更新!javascript
本文主要從以下幾個方面進行講解php
- 存儲的數據庫結構
- GreenDao的優缺點
- GreenDao的使用配置
- 使用GreenDao實現數據的增刪改查
- GreenDao的註解使用
- GreenDao的關係處理
- GreenDao的升級
- GreenDao數據庫加密
- 項目地址AserbaosAndroid
- 總結
- 參考博客
簡書暫時不支持目錄,若是想看該文章帶目錄版本,請點擊跳轉該文章的CSDN地址
我們先看一波最終的效果圖:文章最後有項目地址;css
學習數據庫以前,咱們先得設計本身的數據庫,很少廢話,下面是我這次學習的數據庫結構,後面全部的數據請參考這個圖進行學習:html
簡單的GreenDao的介紹,嫌麻煩的能夠直接跳到GreenDao使用開始看。java
GreenDAO是一個開源的Android ORM(「對象/關係映射」),經過ORM(稱爲「對象/關係映射」),在咱們數據庫開發過程當中節省了開發時間!android
經過GreenDao,咱們能夠更快速的操做數據庫,咱們可使用簡單的面相對象的API來存儲,更新,刪除和查詢Java對象。git
高性能,下面是官方給出的關於GreenDao,OrmLite和ActiveAndroid三種ORM解決方案的數據統計圖:github
易於使用的強大API,涵蓋關係和鏈接;sql
最小的內存消耗;數據庫
小庫大小(<100KB)以保持較低的構建時間並避免65k方法限制;
數據庫加密:greenDAO支持SQLCipher,以確保用戶的數據安全;
GreenDao的核心類有三個:分別是DaoMaster,DaoSession,XXXDao,這三個類都會自動建立,無需本身編寫建立!
Entities :可持久化對象。一般, 實體對象表明一個數據庫行使用標準 Java 屬性(如一個POJO 或 JavaBean )。
要在Android項目中使用GreenDao,您須要添加GreenDao Gradle插件並添加GreenDao庫:
// 在 Project的build.gradle 文件中添加: buildscript { repositories { jcenter() mavenCentral() // add repository } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin } }
// 在 Moudle:app的 build.gradle 文件中添加: apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' // apply plugin dependencies { implementation 'org.greenrobot:greendao:3.2.2' // add library }
greendao {
schemaVersion 1 //數據庫版本號 daoPackage 'com.aserbao.aserbaosandroid.functions.database.greenDao.db' // 設置DaoMaster、DaoSession、Dao 包名 targetGenDir 'src.main.java'//設置DaoMaster、DaoSession、Dao目錄,請注意,這裏路徑用.不要用/ generateTests false //設置爲true以自動生成單元測試。 targetGenDirTests 'src/main/java' //應存儲生成的單元測試的基本目錄。默認爲 src / androidTest / java。 }
配置完成,在Android Studio中使用Build> Make Project,重寫build項目,GreenDao集成完成!
使用GreenDao存儲數據只須要在存儲數據類前面聲明@Entity註解就讓GreenDao爲其生成必要的代碼:
@Entity
public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 ……getter and setter and constructor method…… }
咱們能夠在Application中維持一個全局的會話。咱們在Applicaiton進行數據庫的初始化操做:
/** * 初始化GreenDao,直接在Application中進行初始化操做 */ private void initGreenDao() { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db"); SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); } private DaoSession daoSession; public DaoSession getDaoSession() { return daoSession; }
初始化完成以後從新rebuild一下項目會發如今設置的targetGenDir的目錄生成三個類文件,這個是GreenDao自動生成的!說明數據庫已經鏈接好了,我們接下來只須要進行數據庫的增刪改查操做就好了。Let's Go!
insert() 插入數據
@Override public void insertData(Thing s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); for (int i = 0; i < 1000; i++) { Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); } }
insertOrReplace()數據存在則替換,數據不存在則插入
@Override public void insertData(Thing s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); for (int i = 0; i < 1000; i++) { Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insertOrReplace(student);//插入或替換 } }
刪除有兩種方式:delete()和deleteAll();分別表示刪除單個和刪除全部。
@Override public void deleteData(Student s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); daoSession.delete(s); }
@Override public void deleteAll() { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); daoSession.deleteAll(Student.class); }
經過update來進行修改:
@Override public void updataData(Student s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); daoSession.update(s); }
查詢的方法有:
public List queryAll(){ List<Student> students = daoSession.loadAll(Student.class); return students; }
@Override public void queryData(String s) { List<Student> students = daoSession.queryRaw(Student.class, " where id = ?", s); mDataBaseAdapter.addNewStudentData(students); }
編寫SQL可能很困難而且容易出現錯誤,這些錯誤僅在運行時纔會被注意到。該QueryBuilder的類可讓你創建你的實體,而不SQL自定義查詢,並有助於在編譯時已檢測錯誤。
咱們先講下QueryBuilder的常見方法:
GreenDao中SQL語句的縮寫,咱們也瞭解下,源碼在Property中,使用的時候能夠本身點進去查詢便可:
查詢當前Student表的全部的數據:
public List queryAllList(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); List<Student> list = qb.list(); // 查出全部的數據 return list; }
查詢Name爲「一」的全部Student:
public List queryListByMessage(String name){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); QueryBuilder<Student> studentQueryBuilder = qb.where(StudentDao.Properties.Name.eq("一")).orderAsc(StudentDao.Properties.Name); List<Student> studentList = studentQueryBuilder.list(); //查出當前對應的數據 return list; }
經過原始的SQL查詢語句進行查詢!其實上面有提到QueryBuilder的目的就是方便快捷的編寫SQL查詢語句,避免咱們本身在編寫過程當中出錯!簡單介紹下經過QueryBuilder編寫數據庫,方式方法以下 :
public List queryListBySqL(){ // 查詢ID大於5的全部學生 DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); Query<Student> query = daoSession.queryBuilder(Student.class).where( new WhereCondition.StringCondition("_ID IN " + "(SELECT _ID FROM STUDENT WHERE _ID > 5)") ).build(); List<Student> list = query.list(); return list; }
查詢Id大於5小於10,且Name值爲"一"的數據:
public List queryList(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); qb = daoSession.queryBuilder(Student.class); List<Student> list2 = qb.where(StudentDao.Properties.Name.eq("一"), qb.and(StudentDao.Properties.Id.gt(5), StudentDao.Properties.Id.le(50))).list(); return list2; }
取10條Id大於1的數據,且偏移2條
public List queryListByOther(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); //搜索條件爲Id值大於1,即結果爲[2,3,4,5,6,7,8,9,10,11]; // offset(2)表示日後偏移2個,結果爲[4,5,6,7,8,9,10,11,12,13]; List<Student> list = qb.where(StudentDao.Properties.Id.gt(1)).limit(10).offset(2).list(); return list; }
使用QueryBuilder構建查詢後,能夠重用 Query對象以便稍後執行查詢。這比始終建立新的Query對象更有效。若是查詢參數沒有更改,您能夠再次調用list / unique方法。能夠經過setParameter方法來修改條件參數值:
public List queryListByMoreTime(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); //搜索條件爲Id值大於1,即結果爲[2,3,4,5,6,7,8,9,10,11]; // offset(2)表示日後偏移2個,結果爲[4,5,6,7,8,9,10,11,12,13]; Query<Student> query = qb.where(StudentDao.Properties.Id.gt(1)).limit(10).offset(2).build(); List<Student> list = query.list(); //經過SetParameter來修改上面的查詢條件,好比咱們將上面條件修改取10條Id值大於5,日後偏移兩位的數據,方法以下! query.setParameter(0,5); List<Student> list1 = query.list(); return list1; }
若是在多個線程中使用查詢,則必須調用 forCurrentThread ()以獲取當前線程的Query實例。Query的對象實例綁定到構建查詢的擁有線程。
這使您能夠安全地在Query對象上設置參數,而其餘線程不會干擾。若是其餘線程嘗試在查詢上設置參數或執行綁定到另外一個線程的查詢,則會拋出異常。像這樣,您不須要同步語句。實際上,您應該避免鎖定,由於若是併發事務使用相同的Query對象,這可能會致使死鎖。
每次調用forCurrentThread ()時, 參數都會在使用其構建器構建查詢時設置爲初始參數。
使用QueryBuilder進行批量刪除操做,不會刪除單個實體,但會刪除符合某些條件的全部實體。要執行批量刪除,請建立QueryBuilder,調用其 buildDelete ()方法,而後執行返回的 DeleteQuery。
例子:刪除數據庫中id大於5的全部其餘數據
public boolean deleteItem(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> where = daoSession.queryBuilder(Student.class).where(StudentDao.Properties.Id.gt(5)); DeleteQuery<Student> deleteQuery = where.buildDelete(); deleteQuery.executeDeleteWithoutDetachingEntities(); return false; }
從GreenDao 3 使用註解來定義模型和實體,前面也講過,經過註解的使用能夠快速構建數據庫表,包括設置主鍵,自增,值是否惟一等等等……
下面咱們來看下註解的簡單使用:
@Entity
public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 ……getter and setter and constructor method…… }
@Entity是GreenDao必不可少的註解,只有在實體類中使用了@Entity註解GreenDao纔會建立對應的表。固然咱們也可使用@Entity配置一些細節:
@Entity( schema = "myschema", active = true, nameInDb = "AWESOME_USERS", indexes = { @Index(value = "message DESC", unique = true) }, createInDb = false, generateConstructors = true, generateGettersSetters = true ) public class Student{ …… }
@Id
@Id註解選擇 long / Long屬性做爲實體ID。在數據庫方面,它是主鍵。參數autoincrement = true 表示自增,id不給賦值或者爲賦值爲null便可(這裏須要注意,若是要實現自增,id必須是Long,爲long不行!)。
@Entity public class Student { @Id(autoincrement = true) Long id; …… }
@Property
容許您定義屬性映射到的非默認列名。若是不存在,GreenDAO將以SQL-ish方式使用字段名稱(大寫,下劃線而不是camel狀況,例如 name將成爲 NAME)。注意:您當前只能使用內聯常量來指定列名。
@Entity public class Student { @Id(autoincrement = true) Long id; @Property (nameInDb="name") //設置了,數據庫中的表格屬性名爲"name",若是不設置,數據庫中表格屬性名爲"NAME" String name; …… }
@NotNull :設置數據庫表當前列不能爲空 。
@Transient :添加次標記以後不會生成數據庫表的列。標記要從持久性中排除的屬性。將它們用於臨時狀態等。或者,您也可使用Java中的transient關鍵字。
@Entity public class Student { @Id(autoincrement = true) Long id; @Property(nameInDb="name") @Index(unique = true) String name; …… }
注意: 上面這種狀況,約定name爲惟一值,向數據庫中經過insert方法繼續添加已存在的name數據,會拋異常:
10-08 20:59:46.274 31939-31939/com.example.aserbao.aserbaosandroid E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aserbao.aserbaosandroid, PID: 31939 android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: STUDENT.name (Sqlite code 2067), (OS error - 2:No such file or directory) ……
若使用insertOrReplace()方法添加數據,當前數據庫中不會有重複的數據,可是重複的這條數據的id會被修改!若項目中有用到id字段進行排序的話,這一點須要特別注意。
關係型註解GreenDao中主要就兩個:
日常項目中,咱們常常會使用到多表關聯,如文章開頭所說的數據庫表結構設置的那樣!接下來咱們來說如何經過GreenDao實現多表關聯。
一個學生對應一個身份證號:
作法:
學生Student代碼:
@Entity public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 @ToOne(joinProperty = "name") IdCard student; ……getter and setter …… }
身份證IdCard代碼:
@Entity public class IdCard { @Id String userName;//用戶名 @Unique String idNo;//身份證號 ……getter and setter …… }
insert一組數據:
public void addStudent(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); //插入對應的IdCard數據 IdCard idCard = new IdCard(); idCard.setUserName(userName); idCard.setIdNo(RandomValue.getRandomID()); daoSession.insert(idCard); }
ok,數據能夠了!如今數據庫表插入完成了。
一我的擁有多個信用卡
作法:
Student的代碼:
@Entity public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 @ToMany(referencedJoinProperty = "id") // 這個id是對應在CreditCard中的id List<CreditCard> creditCardsList; ……getter and setter …… }
CreditCard的代碼:
@Entity public class CreditCard { @Id Long id; Long userId; String userName;//持有者名字 String cardNum;//卡號 String whichBank;//哪一個銀行的 int cardType;//卡等級,分類 0 ~ 5 ……getter and setter …… }
添加數據代碼:
public void addStudent(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); //插入對應的CreditCard數據 for (int j = 0; j < random.nextInt(5) + 1 ; j++) { CreditCard creditCard = new CreditCard(); creditCard.setUserId(id); creditCard.setUserName(userName); creditCard.setCardNum(String.valueOf(random.nextInt(899999999) + 100000000) + String.valueOf(random.nextInt(899999999) + 100000000)); creditCard.setWhichBank(RandomValue.getBankName()); creditCard.setCardType(random.nextInt(10)); daoSession.insert(creditCard); } }
一個學生有多個老師,老師有多個學生。
作法:
咱們須要建立一個學生老師管理器(StudentAndTeacherBean),用來對應學生和老師的ID;
咱們須要在學生對象中,添加註解:
@ToMany
@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "studentId",targetProperty = "teacherId")
List<Teacher> teacherList;
咱們須要在老師對象中,添加註解:@ToMany
@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "teacherId",targetProperty = "studentId") List<Student> studentList;
StudentAndTeacherBean代碼:
@Entity public class StudentAndTeacherBean { @Id(autoincrement = true) Long id; Long studentId;//學生ID Long teacherId;//老師ID ……getter and setter …… }
Student 代碼:
@Entity public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 @ToMany @JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "studentId",targetProperty = "teacherId") List<Teacher> teacherList; ……getter and setter …… }
Teacher代碼:
@Entity public class Teacher { @Id(autoincrement = true) Long id; @Unique int teacherNo;//職工號 int age; //年齡 String sex; //性別 String telPhone; String name;//姓名 String schoolName;//學校名字 String subject;//科目 @ToMany @JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "teacherId",targetProperty = "studentId") List<Student> studentList; ……getter and setter …… }
數據添加:
public void addData(){ Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); Collections.shuffle(teacherList); for (int j = 0; j < mRandom.nextInt(8) + 1; j++) { if(j < teacherList.size()){ Teacher teacher = teacherList.get(j); StudentAndTeacherBean teacherBean = new StudentAndTeacherBean(student.getId(), teacher.getId()); daoSession.insert(teacherBean); } } }
好了,成功;
GreenDao的OpenHelper下有個 onUpgrade(Database db, int oldVersion, int newVersion)方法,當設置的數據庫版本改變時,在數據庫初始化的時候就會回調到這個方法,咱們能夠經過繼承OpenHelper重寫onUpgrade方法來實現數據庫更新操做:
GreenDao的升級思路:
ok,思路就是這樣, 總共兩個類: 一個MyDaoMaster(OpenHelper繼承類),一個MigrationHelper(數據庫操做類) 下面是代碼編寫:
修改Application中的DaoMaster的建立:
MyDaoMaster helper = new MyDaoMaster(this, "aserbaos.db"); // DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db"); SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession();
MyDaoMaster代碼:
public class MyDaoMaster extends OpenHelper { private static final String TAG = "MyDaoMaster"; public MyDaoMaster(Context context, String name) { super(context, name); } public MyDaoMaster(Context context, String name, SQLiteDatabase.CursorFactory factory) { super(context, name, factory); } @Override public void onUpgrade(Database db, int oldVersion, int newVersion) { super.onUpgrade(db, oldVersion, newVersion); MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() { @Override public void onCreateAllTables(Database db, boolean ifNotExists) { DaoMaster.createAllTables(db, ifNotExists); } @Override public void onDropAllTables(Database db, boolean ifExists) { DaoMaster.dropAllTables(db, ifExists); } },ThingDao.class); Log.e(TAG, "onUpgrade: " + oldVersion + " newVersion = " + newVersion); } }
MigrationHelper 代碼:
public final class MigrationHelper { public static boolean DEBUG = false; private static String TAG = "MigrationHelper"; private static final String SQLITE_MASTER = "sqlite_master"; private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master"; private static WeakReference<ReCreateAllTableListener> weakListener; public interface ReCreateAllTableListener{ void onCreateAllTables(Database db, boolean ifNotExists); void onDropAllTables(Database db, boolean ifExists); } public static void migrate(SQLiteDatabase db, Class<? extends AbstractDao<?, ?>>... daoClasses) { printLog("【The Old Database Version】" + db.getVersion()); Database database = new StandardDatabase(db); migrate(database, daoClasses); } public static void migrate(SQLiteDatabase db, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) { weakListener = new WeakReference<>(listener); migrate(db, daoClasses); } public static void migrate(Database database, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) { weakListener = new WeakReference<>(listener); migrate(database, daoClasses); } public static void migrate(Database database, Class<? extends AbstractDao<?, ?>>... daoClasses) { printLog("【Generate temp table】start"); generateTempTables(database, daoClasses); printLog("【Generate temp table】complete"); ReCreateAllTableListener listener = null; if (weakListener != null) { listener = weakListener.get(); } if (listener != null) { listener.onDropAllTables(database, true); printLog("【Drop all table by listener】"); listener.onCreateAllTables(database, false); printLog("【Create all table by listener】"); } else { dropAllTables(database, true, daoClasses); createAllTables(database, false, daoClasses); } printLog("【Restore data】start"); restoreData(database, daoClasses); printLog("【Restore data】complete"); } private static void generateTempTables(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { for (int i = 0; i < daoClasses.length; i++) { String tempTableName = null; DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]); String tableName = daoConfig.tablename; if (!isTableExists(db, false, tableName)) { printLog("【New Table】" + tableName); continue; } try { tempTableName = daoConfig.tablename.concat("_TEMP"); StringBuilder dropTableStringBuilder = new StringBuilder(); dropTableStringBuilder.append("DROP TABLE IF EXISTS ").append(tempTableName).append(";"); db.execSQL(dropTableStringBuilder.toString()); StringBuilder insertTableStringBuilder = new StringBuilder(); insertTableStringBuilder.append("CREATE TEMPORARY TABLE ").append(tempTableName); insertTableStringBuilder.append(" AS SELECT * FROM ").append(tableName).append(";"); db.execSQL(insertTableStringBuilder.toString()); printLog("【Table】" + tableName +"\n ---Columns-->"+getColumnsStr(daoConfig)); printLog("【Generate temp table】" + tempTableName); } catch (SQLException e) { Log.e(TAG, "【Failed to generate temp table】" + tempTableName, e); } } } private static boolean isTableExists(Database db, boolean isTemp, String tableName) { if (db == null || TextUtils.isEmpty(tableName)) { return false; } String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER; String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?"; Cursor cursor=null; int count = 0; try { cursor = db.rawQuery(sql, new String[]{"table", tableName}); if (cursor == null || !cursor.moveToFirst()) { return false; } count = cursor.getInt(0); } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) cursor.close(); } return count > 0; } private static String getColumnsStr(DaoConfig daoConfig) { if (daoConfig == null) { return "no columns"; } StringBuilder builder = new StringBuilder(); for (int i = 0; i < daoConfig.allColumns.length; i++) { builder.append(daoConfig.allColumns[i]); builder.append(","); } if (builder.length() > 0) { builder.deleteCharAt(builder.length() - 1); } return builder.toString(); } private static void dropAllTables(Database db, boolean ifExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) { reflectMethod(db, "dropTable", ifExists, daoClasses); printLog("【Drop all table by reflect】"); } private static void createAllTables(Database db, boolean ifNotExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) { reflectMethod(db, "createTable", ifNotExists, daoClasses); printLog("【Create all table by reflect】"); } /** * dao class already define the sql exec method, so just invoke it */ private static void reflectMethod(Database db, String methodName, boolean isExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) { if (daoClasses.length < 1) { return; } try { for (Class cls : daoClasses) { Method method = cls.getDeclaredMethod(methodName, Database.class, boolean.class); method.invoke(null, db, isExists); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private static void restoreData(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { for (int i = 0; i < daoClasses.length; i++) { DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]); String tableName = daoConfig.tablename; String tempTableName = daoConfig.tablename.concat("_TEMP"); if (!isTableExists(db, true, tempTableName)) { continue; } try { // get all columns from tempTable, take careful to use the columns list List<TableInfo> newTableInfos = TableInfo.getTableInfo(db, tableName); List<TableInfo> tempTableInfos = TableInfo.getTableInfo(db, tempTableName); ArrayList<String> selectColumns = new ArrayList<>(newTableInfos.size()); ArrayList<String> intoColumns = new ArrayList<>(newTableInfos.size()); for (TableInfo tableInfo : tempTableInfos) { if (newTableInfos.contains(tableInfo)) { String column = '`' + tableInfo.name + '`'; intoColumns.add(column); selectColumns.add(column); } } // NOT NULL columns list for (TableInfo tableInfo : newTableInfos) { if (tableInfo.notnull && !tempTableInfos.contains(tableInfo)) { String column = '`' + tableInfo.name + '`'; intoColumns.add(column); String value; if (tableInfo.dfltValue != null) { value = "'" + tableInfo.dfltValue + "' AS "; } else { value = "'' AS "; } selectColumns.add(value + column); } } if (intoColumns.size() != 0) { StringBuilder insertTableStringBuilder = new StringBuilder(); insertTableStringBuilder.append("REPLACE INTO ").append(tableName).append(" ("); insertTableStringBuilder.append(TextUtils.join(",", intoColumns)); insertTableStringBuilder.append(") SELECT "); insertTableStringBuilder.append(TextUtils.join(",", selectColumns)); insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";"); db.execSQL(insertTableStringBuilder.toString()); printLog("【Restore data】 to " + tableName); } StringBuilder dropTableStringBuilder = new StringBuilder(); dropTableStringBuilder.append("DROP TABLE ").append(tempTableName); db.execSQL(dropTableStringBuilder.toString()); printLog("【Drop temp table】" + tempTableName); } catch (SQLException e) { Log.e(TAG, "【Failed to restore data from temp table 】" + tempTableName, e); } } } private static List<String> getColumns(Database db, String tableName) { List<String> columns =