How to solve greenDAO "No such table exists error" when doing an InsertOrReplace?
Asked Answered
T

3

3

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));
}
Theater answered 24/6, 2016 at 6:53 Comment(0)
E
6

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

Erie answered 27/3, 2017 at 12:19 Comment(0)
E
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");
Enjoyable answered 6/2, 2017 at 5:55 Comment(0)
S
0

Have you did this?

mSQLiteDatabase = mOpenHelper.getWritableDatabase();
mDaoMaster = new DaoMaster(mSQLiteDatabase);
mDaoSession = mDaoMaster.newSession();
appTimeUsageDao = mDaoSession.getAppTimeUsageDaoDao();
Scanties answered 24/6, 2016 at 6:59 Comment(1)
SQLiteOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), Globals.DatabaseName, null); DaoMaster master = new DaoMaster(helper.getWritableDatabase()); daoSession = master.newSession(); appTimeUsageDao = DeviceInsightApp.getSession(this, true).getAppTimeUsageDao(); Thats what I have in my code and I am not sure why I am getting the errorTheater

© 2022 - 2024 — McMap. All rights reserved.