How to clean/delete greenDao database
Asked Answered
B

6

13

Currently I'm doing it like this:

DaoMaster.dropAllTables(getDb(), true);
DaoMaster.createAllTables(getDb(), true);

but then, when I'm trying to add entity to the database, I'm getting crash log saying that this table isn't exist

Edit1: I know that it happens because the db is locked and tables wasn't created yet. So I'm reducing this problem to the problem - how to know if the tables are locked in grrenDao/Sqlite?

Busiek answered 29/4, 2013 at 8:26 Comment(0)
J
6

Until now, I don't worry if tables are locked or not; in my case, i do the following and it works:

First, when App.onCreate executes, I make the standard initializations.

    T.devOpenHelper= new DaoMaster.DevOpenHelper(context, "mydatabase", null);
    T.sqLiteDatabase= T.devOpenHelper.getWritableDatabase();
    T.daoMaster= new DaoMaster(T.sqLiteDatabase);
    T.daoSession= T.daoMaster.newSession();
    T.dao_myEntity= T.daoSession.getMyEntityDao();

In some moment in the future I drop and recreate all tables, just like you:

    T.daoMaster.dropAllTables(T.sqLiteDatabase, true);
    T.daoMaster.createAllTables(T.sqLiteDatabase, true);

But in my case, then I can immediately insert a new entity:

    MyEntity e= new MyEntity();
    e.setId_ticket(1L);
    e.setDescription("wololo");
    long id= T.dao_myEntity.insert(e);
    Log.d(G.tag, "T.erase_all: id: " + id); // prints "T.erase_all: id: 1"

I hope it helps.

Judyjudye answered 7/9, 2015 at 1:24 Comment(1)
Excuse me but what is T here ?Bibliophile
K
14

How about using something like this for each table?

daoSession.getSometableDao().deleteAll();
Knead answered 10/9, 2013 at 14:42 Comment(1)
You might forget to add extra calls if you add new tables in the future...Eogene
J
6

Until now, I don't worry if tables are locked or not; in my case, i do the following and it works:

First, when App.onCreate executes, I make the standard initializations.

    T.devOpenHelper= new DaoMaster.DevOpenHelper(context, "mydatabase", null);
    T.sqLiteDatabase= T.devOpenHelper.getWritableDatabase();
    T.daoMaster= new DaoMaster(T.sqLiteDatabase);
    T.daoSession= T.daoMaster.newSession();
    T.dao_myEntity= T.daoSession.getMyEntityDao();

In some moment in the future I drop and recreate all tables, just like you:

    T.daoMaster.dropAllTables(T.sqLiteDatabase, true);
    T.daoMaster.createAllTables(T.sqLiteDatabase, true);

But in my case, then I can immediately insert a new entity:

    MyEntity e= new MyEntity();
    e.setId_ticket(1L);
    e.setDescription("wololo");
    long id= T.dao_myEntity.insert(e);
    Log.d(G.tag, "T.erase_all: id: " + id); // prints "T.erase_all: id: 1"

I hope it helps.

Judyjudye answered 7/9, 2015 at 1:24 Comment(1)
Excuse me but what is T here ?Bibliophile
L
2
public static void clearDatabase(Context context) {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(
                context.getApplicationContext(), Constants.SQL_DB_NAME, null);
        SQLiteDatabase db = devOpenHelper.getWritableDatabase();
        devOpenHelper.onUpgrade(db,0,0);
    }
Lamson answered 18/4, 2016 at 6:45 Comment(0)
M
2

For now it can be done like that:

for (AbstractDao abstractDao : mDaoSession.getAllDaos()){
    abstractDao.deleteAll();
}
Mucronate answered 23/10, 2017 at 17:2 Comment(0)
N
0

I upgraded the schemaVersion in build.gradle to pass through this error

Nightfall answered 17/7, 2018 at 14:23 Comment(0)
D
-2

Try this one:

QueryBuilder<cart> qb = SQLConfig.cartDao.queryBuilder();
List<cart> mUpadateData = qb.where(cartDao.Properties.Product_sku.eq(skuApi)).list();
SQLConfig.cartDao.deleteInTx(mUpadateData);
Detriment answered 20/4, 2016 at 11:2 Comment(1)
Its only clean cartDao tables.Terminus

© 2022 - 2024 — McMap. All rights reserved.