删除Sugar ORM

时间:2016-07-22 21:13:01

标签: android android-sqlite sugarorm

我在我的应用程序中使用Sugar ORM 1.5,我希望在用户注销时清除所有信息。

我想避免删除数据库文件或尽可能使用纯SQLite脚本。

我找到的唯一解决方案是在所有表上使用deleteAll,但我想知道是否有更好的方法来执行此操作。

此致

修改

我解决了删除数据库之前删除数据库的问题,然后再删除数据库SugarContext.terminate();

看起来这是Henry Dang在评论中指出的最佳解决方案,它比删除所有数据更快。

3 个答案:

答案 0 :(得分:10)

这个代码段对我有用。它终止上下文删除所有表并重新创建它们:

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。

答案 1 :(得分:1)

for other users who just want to delete all records (not tables or database)

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

答案 2 :(得分:0)

  

如果我们要删除序列,也可以在for循环中添加此行

            SugarRecord.executeQuery("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + NamingHelper.toSQLName(domain) + "'");