How to update column with ORMLite

22,111

Solution 1

I want to update catType on the basis of catId. How can I update this catType column with the catId in ORMLite.

ORMLite also supports an UpdateBuilder. Here's how you could use it:

UpdateBuilder<Category, Integer> updateBuilder = catDao.updateBuilder();
// set the criteria like you would a QueryBuilder
updateBuilder.where().eq("categoryId", 5);
// update the value of your field(s)
updateBuilder.updateColumnValue("catType" /* column */, "BarType" /* value */);
updateBuilder.update();

See the UpdateBuilder docs for more examples.

Solution 2

Dao<Category, Integer> catDao = getHelper().getCategoryDao();
List <Category> categoryList = catDao.queryForAll();
for (Category c: categoryList ) {
    c.setCategoryType("BarType");
    catDao.update(c);
}
Share:
22,111
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I have an already created database for Android application and I'm using ORMLite for query to SQLLite.

    I have added a column in category and I want to insert data in that column. I have row, e.g.

    catId=5 catname="food" catType=?
    

    I want to update catType on the basis of catId. How can I update this catType column with the catId in ORMLite.

    I am using this approach:

    Dao<Category, Integer> catDao = getHelper().getCategoryDao();
    QueryBuilder<Category, Integer> queryBuilder = catDao.queryBuilder();
    queryBuilder.where().eq("categoryId", 5);
    PreparedQuery<Category> pq = queryBuilder.prepare();
    Category category = catDao.queryForFirst(pq);
    category.setCategoryType("BarType");
    catDao.createOrUpdate(category);
    

    Please provide me a better solution.

  • Mattan
    Mattan almost 9 years
    Concurrent updates to the same entity (but on different fields) may override each others.