Android 如何使用 ORMLite 更新列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12497287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:49:16  来源:igfitidea点击:

How to update column with ORMLite

androidsqliteormlite

提问by khus_android

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

我已经为 Android 应用程序创建了一个数据库,我正在使用 ORMLite 查询 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 catTypeon the basis of catId. How can I update this catTypecolumn with the catIdin ORMLite.

我想在catType.的基础上更新catId。如何catType使用catIdORMLite更新此列。

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.

请给我一个更好的解决方案。

回答by Gray

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

我想根据 catId 更新 catType。如何使用 ORMLite 中的 catId 更新此 catType 列。

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

ORMLite还支持UpdateBuilder. 以下是您可以如何使用它:

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 UpdateBuilderdocsfor more examples.

有关更多示例,请参阅UpdateBuilder文档

回答by buxik

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