How to solve greenDAO “No such table exists error” when doing an InsertOrReplace?

人盡茶涼 提交于 2020-01-03 21:15:33

问题


I am using greenDAO and I have successfully generated all necessary classes and entities and I can see that my table has been created, however after putting breakpoints on the line to replace, I get an error telling me "No such table exists error".

try {
    appTimeUsageDao.insertOrReplace(appStats);
//} catch (DaoException  e) {
} catch (Exception e) {
    Log.e("Error", "Some exception occurred", e);
    Log.e("APP_TAG", Log.getStackTraceString(e));
}

回答1:


For me this issue was related to this allowBackup flag in the manifest.

This functionality was added from api 23 onwards and the effect of it is to restore the device database even when the app has been uninstalled, so if you're trying to clear the database by uninstalling it wont work as Android restores it, similar to how iCloud works.

I could be missing somewhere in the documentation that explains this error but it isn't clear to me that this could be an issue in GreenDao 3. Additionally as many users will set up a test entity and not consider handling the upgrade path as they have no desire to retain the test table, which results in the scenario of a single table restored and the new tables not being created.

So essentially if you're just testing set the flag to false otherwise handle the upgrade flow. (the flag defaults to true!)

https://developer.android.com/guide/topics/data/autobackup.html




回答2:


I followed this guide and was having the same problem. I had the database name wrong, for some reason. Check that they are named the same in the AndroidManifest.xml file:

<meta-data
        android:name="DATABASE"
        android:value="notes.db"/>

And in your class that extends Application:

DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes.db");



回答3:


Have you did this?

mSQLiteDatabase = mOpenHelper.getWritableDatabase();
mDaoMaster = new DaoMaster(mSQLiteDatabase);
mDaoSession = mDaoMaster.newSession();
appTimeUsageDao = mDaoSession.getAppTimeUsageDaoDao();


来源:https://stackoverflow.com/questions/38007294/how-to-solve-greendao-no-such-table-exists-error-when-doing-an-insertorreplace

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