How to clean/delete greenDao database

半城伤御伤魂 提交于 2019-12-01 02:54:50

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.

How about using something like this for each table?

daoSession.getSometableDao().deleteAll();
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);
    }

For now it can be done like that:

for (AbstractDao abstractDao : mDaoSession.getAllDaos()){
    abstractDao.deleteAll();
}

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

Mohan K

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!