0

How to update a table using a key(not primary id key)?

I have a customer table and I am getting customer data which are updated or created. So, for newly created data I am using insertOrReplace method which is inserting the data using the object.

But for updating a row, as per my knowledge I have to get the row using the key I have and then update the row. Is it not possible to do it without getting the row as that will require two operations for each such row.

Anuj
  • 1,160
  • 2
  • 20
  • 40

1 Answers1

1

As you can always write your own sql-statements and execute them via DaoSesseion.getDatabase() it is possible to update your entry without fetching it first.

Since greendao uses a cache I would strongly discourage updating it manually, since your cache won't be updated and this in turn may produce very strange behaviour of your app.

On top of that, the performance penalty for fetching your entry won't be noticeable.

Summarized:

Updating manually makes your code more complex, may introduce cache inconsistency and does only provide a very small performance boost (if it boosts the performance at all). Hence IMHO don't update entries manually!

AlexS
  • 5,295
  • 3
  • 38
  • 54
  • The reason I'm asking this is because I am getting multiple entries and need to update few of them. I wanted to use something similar to updateInTx for that. Anyways, thanks for explanation about greendao cache. – Anuj Jun 30 '14 at 09:49
  • 1
    @Anuj If you want all your updates to occur in one transaction you just have to start a transaction before your first update and commit it or roll it back after your last update / error. Greendao will recognize your transaction and use it. See my answer [here](http://stackoverflow.com/questions/23567985/android-java-outofmemory-while-inserting-a-huge-list-into-the-database-sql/23604766#23604766). – AlexS Jun 30 '14 at 10:03