Search code examples
androiddatabaselistviewsugarorm

Delete Sugar ORM database table records


I have a listview with some name and url.. After my app starts I check if there is any record in my local database. So if there is, First clear my old records then fill table columns with listview data and if there isn't just insert data... this is how I'm doing it :

List<LocalProduct> allAuthors = LocalProduct.listAll(LocalProduct.class);
if (allAuthors == null) {
    for (int allList=0;allList<adapter.getCount();allList++){
        Product my = adapter.getItem(allList);
        String offline_name = my.name.toString().trim();
        String offline_url = my.image_url.toString().trim();
        LocalProduct book = new LocalProduct(offline_name, offline_url);
        book.save();
        }
    }else {

        /*
        SugarContext.terminate();
        SchemaGenerator schemaGenerator = new SchemaGenerator(getContext());
        schemaGenerator.deleteTables(new SugarDb(getContext()).getDB());
        SugarContext.init(getContext());
        schemaGenerator.createDatabase(new SugarDb(getContext()).getDB());
        */

    for (int allList=0;allList<adapter.getCount();allList++){
        Product my = adapter.getItem(allList);
        String offline_name = my.name.toString().trim();
        String offline_url = my.image_url.toString().trim();
        LocalProduct book = new LocalProduct(offline_name, offline_url);
        book.save();
        }
    }

Commented area of my code will Delete and recreate the tables and this will cause a delay every time... How can I just delete my records !?


Solution

  • For resetting records :

    LocalProduct.deleteAll(LocalProduct.class);
    

    For resetting the autoincrement counter :

    LocalProduct.executeQuery("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + tableName + "'");