从 Sugar ORM 中的所有表中删除所有数据
Delete all data from all tables in Sugar ORM
我在我的应用程序中使用 Sugar ORM 1.5,我想在用户注销时清除所有信息。
我想避免删除数据库文件或尽可能使用纯 SQLite 脚本。
我找到的唯一解决方案是对所有表使用 deleteAll,但我想知道是否有更好的方法。
此致,
编辑
我解决了删除数据库的问题,只是在删除数据库之前调用 SugarContext.terminate();
并在删除数据库之后调用 SugarContext.init(context);
。
看来是Henry Dang在评论中指出的最佳解决方案,而且比删除所有数据更快。
这段代码适合我。它终止上下文删除所有表并重新创建它们:
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。
对于只想删除所有记录(不是表或数据库)的其他用户
public static void deleteAllrecords(Context applicationContext) {
List<Class> domainClasses = getDomainClasses(applicationContext);
for (Class domain : domainClasses) {
SugarRecord.deleteAll(domain);
}
}
if we want to delete sequence also you can add this line inside for loop
SugarRecord.executeQuery("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + NamingHelper.toSQLName(domain) + "'");
我在我的应用程序中使用 Sugar ORM 1.5,我想在用户注销时清除所有信息。
我想避免删除数据库文件或尽可能使用纯 SQLite 脚本。
我找到的唯一解决方案是对所有表使用 deleteAll,但我想知道是否有更好的方法。
此致,
编辑
我解决了删除数据库的问题,只是在删除数据库之前调用 SugarContext.terminate();
并在删除数据库之后调用 SugarContext.init(context);
。
看来是Henry Dang在评论中指出的最佳解决方案,而且比删除所有数据更快。
这段代码适合我。它终止上下文删除所有表并重新创建它们:
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。
对于只想删除所有记录(不是表或数据库)的其他用户
public static void deleteAllrecords(Context applicationContext) {
List<Class> domainClasses = getDomainClasses(applicationContext);
for (Class domain : domainClasses) {
SugarRecord.deleteAll(domain);
}
}
if we want to delete sequence also you can add this line inside for loop
SugarRecord.executeQuery("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + NamingHelper.toSQLName(domain) + "'");