Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean/delete greenDao database

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?

like image 850
Nativ Avatar asked Apr 29 '13 08:04

Nativ


4 Answers

How about using something like this for each table?

daoSession.getSometableDao().deleteAll();
like image 131
demaksee Avatar answered Nov 08 '22 05:11

demaksee


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.

like image 6
jsanmarb Avatar answered Nov 08 '22 05:11

jsanmarb


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);
    }
like image 2
Debanjan Avatar answered Nov 08 '22 05:11

Debanjan


For now it can be done like that:

for (AbstractDao abstractDao : mDaoSession.getAllDaos()){
    abstractDao.deleteAll();
}
like image 2
Ivan Syabro Avatar answered Nov 08 '22 03:11

Ivan Syabro