问题描述
目前我正在这样做:
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
我知道发生这种情况是因为数据库已锁定并且尚未创建表.因此,我将这个问题简化为问题-如何知道表是否在grrenDao/Sqlite中锁定了?
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?
推荐答案
直到现在,我都不担心表是否被锁定;就我而言,我可以执行以下操作,并且可以正常工作:
Until now, I don't worry if tables are locked or not; in my case, i do the following and it works:
首先,当执行App.onCreate时,我将进行标准初始化.
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"
希望对您有帮助.
这篇关于如何清理/删除greenDao数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!