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.
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.
Dao<Category, Integer> catDao = getHelper().getCategoryDao();
List <Category> categoryList = catDao.queryForAll();
for (Category c: categoryList ) {
c.setCategoryType("BarType");
catDao.update(c);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With