Question

Using GreenDao, I've somehow managed to put my db in a state where I have an entity that has no key.

My questions is how do I clear / delete this entity? I didn't see a function in the AbstractDao that will let me delete on another column name.

Was it helpful?

Solution

You can write a raw SQL-query to delete the row and execute it using the SQLiteDatabase that you can get from your readable DaoSession.

DELETE FROM YourTable WHERE someColumn=?

If you want to avoid typos you can use tableinformation from greendao:

String query = "DELETE FROM " + YourDao.TABLENAME +
               " WHERE " + YourDao.Properties.SomeColumn.columnName + "=?";

For further information:

Depending on how you created your schema, there are some pitfalls:

If you created the primary key with AUTOINCREMENT and NOT NULL greendao will not insert the primary key correctly!

Insted of assigning an autoincremented primary key, the primary key will always be 0 resulting in an contraint violation upon inserting a second row into the table.

Therefore you shouldn't use notNull().primaryKey().autoincrement(), if you don't want to have to deal with greendao-sourcecode.

I have made some minor changes to greendao-core to be able to use the upper statement, but everything should work well, if only use primaryKey().autoincrement().

OTHER TIPS

Primary key is itself unique and not null and even if I mention it explicitly (not null().primaryKey()) , it should work flawlessly. I want to point out, whether am I doing wrongly or is there a fault in greenDAO. Is there any word from greenDAO to resolve the issue in the upcoming releases.

I did not have much reputation otherwise i would have asked it in a comment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top