How to clear all tables in ActiveAndroid?

I have about 20-30 tables in my ActiveAndroid database. When I click logoutButton , I want to clear all the tables. How to do it in ActiveAndroid ?

  public void logOut() { //clear all tables } 
+6
source share
3 answers
  SQLiteDatabase db = ActiveAndroid.getDatabase(); List<String> tables = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table';", null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { String tableName = cursor.getString(1); if (!tableName.equals("android_metadata") && !tableName.equals("sqlite_sequence")) { tables.add(tableName); } cursor.moveToNext(); } cursor.close(); for (String tableName : tables) { db.execSQL("DELETE FROM " + tableName); } 
+10
source

Try the following:

  public void logOut() { new Delete().from(MyClass.class).executeSingle(); } 
+5
source

Hope this helps someone who uses Active Android.

there is an easy way to delete all records in a table.

1) First create the "TruncatableModel" class and extend all your models to this class

 public abstract class TruncatableModel extends Model { public static void truncate(Class<? extends Model> type){ final String tableName = Cache.getTableInfo(type).getTableName(); // Delete all rows from table ActiveAndroid.execSQL(String.format("DELETE FROM %s;", tableName)); // Reset ids ActiveAndroid.execSQL(String.format("DELETE FROM sqlite_sequence WHERE name='%s';", tableName)); } } 

..so after extension my model class will look like.

 @Table(name = "note") public class Note extends TruncatableModel { @Column(name ="idn") public Integer idn; @Column(name ="title") public String title; ... } 

2) Now I can delete all entries in the "note" database using this code.

 Note.truncate(Note.class); 

This seems like a more efficient way than any other methods I've tried.

0
source

Source: https://habr.com/ru/post/987628/


All Articles