Zidane
Zidane

Reputation: 1904

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

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));
}

Upvotes: 3

Views: 3861

Answers (3)

Michael.
Michael.

Reputation: 1000

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

Upvotes: 6

Santiago Podestá
Santiago Podestá

Reputation: 51

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");

Upvotes: 2

thealeksandr
thealeksandr

Reputation: 1736

Have you did this?

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

Upvotes: 0

Related Questions