OrmLite delete failed
Question
I am performing a simple delete of a ormlite object. The code is below. But no matter what, the object never get deleted.
Dao<Subscription, Object> dao = helper.getSubscriptionDataDao();
dao = helper.getSubscriptionDataDao();
QueryBuilder<Subscription, Object> query = dao.queryBuilder();
query.where().eq("key", subKey);
Subscription sub = dao.queryForFirst(query.prepare());
int r = dao.delete(sub); // r is 1
Am I missing anything here?
Solution
This is how I solve the problem: Make sure the object have primary key. Setting the field as ID does not mean the ID will be auto generated. Without the ID, delete will fail.
OTHER TIPS
You must create DeleteBuilder
for Delete.QueryBuilder
just for get Query for Select .
public int deleteByObjectId(int objectId) throws SQLException, java.sql.SQLException
{
DAL<T> helper= new DAL<T>(_context, type);
RuntimeExceptionDao<T, Integer> dao =helper.getSimpleDataDao2();
DeleteBuilder<T, Integer> delBuilder =dao.deleteBuilder();
delBuilder.where().eq("ObjId", objectId);
PreparedDelete<T> prepareQery=delBuilder.prepare();
int i=dao.delete(prepareQery);
return i;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow