我需要删除ORMLite数据库我可以用如下的ID删除记录的记录
@Override
public void Delete(int id) throws SQLException {
this.dao.deleteById(id);
}
但如果我没有通过ID,但通过名称或任何其他领域,以删除同一表中的记录我想是这样
public void Deletefromcanteen(String name,MealType mealtype) {
this.dao.deletebyName(name);
}
什么样的查询我应该写使用的QueryBuilder删除其中name =名称和mealtype =说的记录(午餐)
我想这样的事情在我的databasehelper类
public void deletefromcanteen(int id, String mealtype) {
try {
Dao<CanteenLog, Integer> canteenDao = getCanteen();
DeleteBuilder<CanteenLog, Integer> deleteBuilder = canteenDao
.deleteBuilder();
deleteBuilder.where().eq("user_id", id).and().eq("meal", mealtype);
canteenDao.delete(deleteBuilder.prepare());
} catch (Exception e) {
...
}
}
deleteBuilder.where().eq("FIELD_NAME", arg);
deleteBuilder.delete();
更新:
例如 :
//Get helper
DatabaseHelper helper = OpenHelperManager.getHelper(App.getContext(), DatabaseHelper.class);
//get dao
Dao dao = helper.getDao(YOUR_CLASS.class);
//delete elements from table in field by arg
DeleteBuilder<CanteenLog, Integer> deleteBuilder = dao.deleteBuilder();
deleteBuilder.where().eq("FIELD_NAME", arg);
deleteBuilder.delete();
祝好运!
要保存构建查询,你可以做一个选择,找到ID,然后通过ID做了删除。 Thsi将是简单的,如果你已经有了选择查询设置此表。
它也可以做这样的
DatabaseHelper.getInstance().getDao(YourObject.class).delete(yourObjectInstance);
如果您使用的科特林,你需要做这样的事情,而不是因为类型推断不正确的当前工作的流畅的语法:
val deleteBuilder = dao.deleteBuilder()
val where = deleteBuilder.where().eq("address", address)
deleteBuilder.setWhere(where)
deleteBuilder.delete()
如果你已经有了对象(或对象列表)的一个实例,您要删除,只是做:
//Get helper
DatabaseHelper helper =
OpenHelperManager.getHelper(App.getContext(), DatabaseHelper.class);
// delete 'em !
helper.getDao(YourObject.class).delete(yourObjectInstance);
如果你没有实例删除或你想这样做,SQL方式(!):
//Get helper
DatabaseHelper helper =
OpenHelperManager.getHelper(App.getContext(), DatabaseHelper.class);
//get dao
Dao dao = helper.getDao(YOUR_CLASS.class);
//delete elements from table in field by arg
DeleteBuilder<CanteenLog, Integer> deleteBuilder = dao.deleteBuilder();
deleteBuilder.where().eq("FIELD_NAME", "FIELD_VALUE");
deleteBuilder.delete();
如果你需要一个更复杂Where
的逻辑,然后使用deleteBuilder.where().or()
或deleteBuilder.where().and()
以增加更多的限制。