Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update column with ORMLite

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.

like image 363
khus_android Avatar asked Sep 19 '12 14:09

khus_android


2 Answers

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.

like image 54
Gray Avatar answered Oct 19 '22 03:10

Gray


Dao<Category, Integer> catDao = getHelper().getCategoryDao();
List <Category> categoryList = catDao.queryForAll();
for (Category c: categoryList ) {
    c.setCategoryType("BarType");
    catDao.update(c);
}
like image 43
buxik Avatar answered Oct 19 '22 04:10

buxik