How to update column with ORMLite

khus_android picture khus_android · Sep 19, 2012 · Viewed 21.7k times · Source

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.

Answer

Gray picture Gray · Sep 21, 2012

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.