Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update all columns in ormlite database table in Android

Tags:

android

orm

I have the following database table object:

public class Goal {
    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField
    private String goal_title;
    @DatabaseField
    private String goal_desc;
    @DatabaseField
    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.

like image 435
Munazza Avatar asked Jun 14 '12 19:06

Munazza


1 Answers

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 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
updateBuilder.update();

Hope this helps.

like image 147
Gray Avatar answered Sep 28 '22 08:09

Gray