greenrobot / greenDAO

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.
http://greenrobot.org/greendao/
12.63k stars 2.89k forks source link

How to update an record with where condition? #126

Closed vishalkharat123 closed 10 years ago

vishalkharat123 commented 10 years ago

As we can execute an "Update " statement on database with where condition, but those changes are not reflected in corresponding Dao.

greenrobot commented 10 years ago

Updates are only done using entities. If you do stuff outside of entities directly in the DB, you can either refresh entities or clear the session.

vishalkharat123 commented 10 years ago

Thanks for the quick reply.

Whatever you are saying is okay in the case in which I know which entity to update. My scenario is like: I have Person table: I want to update record of person with id = 10. This i can do with "UPDATE PERSON SET NAME = 'VISHAL' WHERE ID = 10";

To achieve this with GreenDao i will have to first fetch record of person with id 10, and then update entity. This will require 2 queries to execute.

How can we achieve this?

greenrobot commented 10 years ago

Works as designed. It's object based, not property based.

vishalkharat123 commented 10 years ago

"Updates are only done using entities. If you do stuff outside of entities directly in the DB, you can either refresh entities or clear the session."

Even if i refresh entities i.e getAllData from dao for that entity (using list and listLazyUncached()) i still get old data not the updated one. Am i missing something ?

greenrobot commented 10 years ago

This is not a support site, this an issue tracker. Please use stackoverflow. Thanks

duongphuhiep commented 10 years ago

Google search point to this page with no answer, so I will make a small one here for others

String updateQuery = "update "+PersonDao.TABLENAME

mDaoMaster.getDatabase().execSQL(updateQuery, new Object[] {"VISHAL", 10});

Donnot forget to refresh your entity: personDao.refresh(person10)

IMO, GreenDao is a good helper for most database tasks, but it is not a silver bullet, you should always master your database and all the query yourself,

rajajawahar commented 8 years ago

How to update a Row in a table by it value not by Id

zhangmingxingAtGit commented 8 years ago

How to update a Row in a table by it value not by Id?

Axrorxoja commented 8 years ago

how to update by id

hzw1199 commented 7 years ago

How to update a Row in a table by it value not by Id?

greenrobot-team commented 7 years ago

Read about how to update an entity in the new docs.

To get an entity, you can query for it or get it by its key using dao.load(key). Check the methods on your DAO class for additional ways to get a hold of an entity. -ut

jp1017 commented 7 years ago

How to update a Row in a table by it value not by Id?

  1. query
  2. update