如何删除一个记录,从ORMLITE?

时间:2022-11-19 01:54:09

I need to delete a record from ORMLite Database I can delete a record by id using as below

我需要删除一个记录,从ORMLite数据库我可以删除一个记录的id使用如下。

@Override
public void Delete(int id) throws SQLException {
    this.dao.deleteById(id);
}

but what if I have to delete a record from same table not by id but by name or any other field I want something like

但是,如果我必须删除来自同一表的记录,而不是id,而是通过名称或其他任何我想要的字段。

public void Deletefromcanteen(String name,MealType mealtype) {
    this.dao.deletebyName(name);
}

what query should i write using querybuilder to delete a record where name = name and mealtype = say (lunch)

我应该使用querybuilder编写什么查询来删除一个记录,其中name = name和mealtype = say(午餐)

I tried something like this in my databasehelper class

我在我的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) {
        ...
    }
}

5 个解决方案

#1


68  

deleteBuilder.where().eq("FIELD_NAME", arg);
deleteBuilder.delete();

Update:

更新:

For example :

例如:

//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();

Good luck!

好运!

#2


0  

To save building a query, you can do a select to find the ID, then do a delete by ID. Thsi will be straightforward if you already have the select query set up for this table.

为了保存构建查询,您可以选择查找ID,然后按ID进行删除。如果您已经为该表设置了select查询,那么Thsi将非常简单。

#3


0  

It can be done also like that

也可以这样做。

DatabaseHelper.getInstance().getDao(YourObject.class).delete(yourObjectInstance);

#4


0  

If you are using Kotlin, you will need to do something like this instead since the type inference is not currently working correctly for the fluent syntax:

如果您使用的是Kotlin,您将需要做类似的事情,因为类型推断目前还没有正确地为流畅的语法工作:

val deleteBuilder = dao.deleteBuilder()
            val where = deleteBuilder.where().eq("address", address)
            deleteBuilder.setWhere(where)
            deleteBuilder.delete()

#5


0  

If you already have an instance of the object (or object list) you want to delete, just do :

如果您已经有了要删除的对象(或对象列表)的实例,请执行以下操作:

//Get helper
DatabaseHelper helper = 
    OpenHelperManager.getHelper(App.getContext(), DatabaseHelper.class);
// delete 'em !
helper.getDao(YourObject.class).delete(yourObjectInstance);

If you don't have the instance to delete or you want to do it SQL way(!):

如果您没有删除实例,或者您想要使用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();

If you need a more complicated Where logic, then use deleteBuilder.where().or() or deleteBuilder.where().and() to add more constraints.

如果您需要一个更复杂的逻辑,那么使用deleteBuilder.where()或()或deleteBuilder.where()和()来添加更多的约束。

#1


68  

deleteBuilder.where().eq("FIELD_NAME", arg);
deleteBuilder.delete();

Update:

更新:

For example :

例如:

//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();

Good luck!

好运!

#2


0  

To save building a query, you can do a select to find the ID, then do a delete by ID. Thsi will be straightforward if you already have the select query set up for this table.

为了保存构建查询,您可以选择查找ID,然后按ID进行删除。如果您已经为该表设置了select查询,那么Thsi将非常简单。

#3


0  

It can be done also like that

也可以这样做。

DatabaseHelper.getInstance().getDao(YourObject.class).delete(yourObjectInstance);

#4


0  

If you are using Kotlin, you will need to do something like this instead since the type inference is not currently working correctly for the fluent syntax:

如果您使用的是Kotlin,您将需要做类似的事情,因为类型推断目前还没有正确地为流畅的语法工作:

val deleteBuilder = dao.deleteBuilder()
            val where = deleteBuilder.where().eq("address", address)
            deleteBuilder.setWhere(where)
            deleteBuilder.delete()

#5


0  

If you already have an instance of the object (or object list) you want to delete, just do :

如果您已经有了要删除的对象(或对象列表)的实例,请执行以下操作:

//Get helper
DatabaseHelper helper = 
    OpenHelperManager.getHelper(App.getContext(), DatabaseHelper.class);
// delete 'em !
helper.getDao(YourObject.class).delete(yourObjectInstance);

If you don't have the instance to delete or you want to do it SQL way(!):

如果您没有删除实例,或者您想要使用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();

If you need a more complicated Where logic, then use deleteBuilder.where().or() or deleteBuilder.where().and() to add more constraints.

如果您需要一个更复杂的逻辑,那么使用deleteBuilder.where()或()或deleteBuilder.where()和()来添加更多的约束。