Munazza Munazza - 1 year ago 99
Android Question

Update all columns in ormlite database table in Android

I have the following database table object:

public class Goal {
@DatabaseField(generatedId = true)
private int id;
private String goal_title;
private String goal_desc;
private String goal_why;

I have added some rows to this table and now I want to write a query to update all the columns of a row in this table. I have seen documentation of ORM and could not get an idea how to write this query. Please help me how to write this query.

Answer Source

I think you need to RTFM. I've spent a long time on the ORMLite documentation and I think it covers the UpdateBuilder pretty well. Feel free to ask more specific questions and I can add more details if not.

To quote from the docs:

The DAO can also be used to construct custom UPDATE and DELETE statements. Update statements are used to change certain fields in rows from the table that match the WHERE pattern - or update all rows if no where(). Delete statements are used to delete rows from the table that match the WHERE pattern - or delete all rows if no where().

To tweak the example code to use your Goal object:

UpdateBuilder<Goal, Integer> updateBuilder = goalDao.updateBuilder();
// update the goal_title and goal_why fields
updateBuilder.updateColumnValue("goal_title", "some other title");
updateBuilder.updateColumnValue("goal_why", "some other why");
// but only update the rows where the description is some value
updateBuilder.where().eq("goal_desc", "unknown description");
// actually perform the update

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download