2016-07-22 97 views
3

我在我的應用程序中使用了Sugar ORM 1.5,我想在用戶註銷時清除所有信息。從Sugar ORM中刪除所有表中的所有數據

我想避免刪除數據庫文件,或者儘可能使用純SQLite腳本。

我發現的唯一解決方案是在所有表上使用deleteAll,但我想知道是否有更好的方法來實現它。

問候,

編輯

我解決這個問題我已經刪除刪除數據庫和SugarContext.init(context);前後只調用SugarContext.terminate();數據庫。

看起來像Henry Dang在評論中指出的那樣是最好的解決方案,它比刪除所有數據更快。

+1

如果你的目標是要刪除的應用程序中的所有數據,那麼其他用戶不會刪除數據庫文件是最直觀的方案 –

+0

我有問題要刪除數據庫,它只在我重新啓動應用程序時刪除數據庫。另一方面,我想更多地瞭解Sugar的數據處理能力 –

回答

9

這段代碼適合我。它終止上下文刪除所有表並重新創建它們:

SugarContext.terminate(); 
SchemaGenerator schemaGenerator = new SchemaGenerator(getApplicationContext()); 
schemaGenerator.deleteTables(new SugarDb(getApplicationContext()).getDB()); 
SugarContext.init(getApplicationContext()); 
schemaGenerator.createDatabase(new SugarDb(getApplicationContext()).getDB()); 

SchemaGenerator是com.orm,我SugarORM的版本是1.5。

+0

工程就像一個魅力。我感謝你。 –

+1

刪除和重新創建表格會導致每次延遲...是否有任何解決方案只是清除記錄! –

1

對誰只是想刪除所有記錄(未表或數據庫)

public static void deleteAllrecords(Context applicationContext) { 
    List<Class> domainClasses = getDomainClasses(applicationContext); 
    for (Class domain : domainClasses) { 
     SugarRecord.deleteAll(domain); 
    } 
}