how to delete a record from ORMLITE?

2019-02-11 17:20发布

问题:

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

@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

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)

I tried something like this in my databasehelper class

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

回答1:

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:

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.



回答3:

It can be done also like that

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


回答4:

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:

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


回答5:

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(!):

//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.