I'm using ORMLite but when I do prints after deletes for exemple I still see the deleted lines, for exemple:
demandesDao = db.getDemandesDao();
final List<Demandes> demandes = demandesDao.queryForAll();
DeleteBuilder<Demandes,Integer> deleteBuilder = demandesDao.deleteBuilder();
deleteBuilder.where().eq("contactWebId", 15515);
deleteBuilder.delete();
System.out.println(demandes);
I'm using ORMLite but when I do prints after deletes for example I still see the deleted lines
That's correct. The DeleteBuilder only removes rows from the database. Any local collections that you have already queried for are not affected. In your case, your demandes list was queried before the delete was performed. If you run another queryForAll() method, you should see that the rows have been removed from the database.
Related
Trying to delete all records from table on a already built app. I has ormlite dao and the function seems very simple
allUsers have a collection of objects to be deleted but i get an Exception :-
SQLException
"java.sql.SQLException: Unable to run delete collection stmt: DELETE FROM user WHERE PIN IN ?,?,?,?,?,)
I tried creating a list of ids and using another option "deleteById" same error
Collection<UserModel> allUsers = helper.getUserDao().queryForAll();
helper.getUserDao().delete(allUsers);
I just need the table to be wiped out.
try it
helper.getUserDao().deleteBuilder().where().eq($yourKey, $yourValue).delete()
I tried creating a list of ids and using another option "deleteById" same error
Collection<UserModel> allUsers = helper.getUserDao().queryForAll();
helper.getUserDao().delete(allUsers);
I'm not sure why this didn't work. If you show more of the exception there may be Caused by sections of the exception that provided more details. It could be that the UserModel doesn't have an ID field?
I just need the table to be wiped out.
There are a couple of ways to do this. The most efficient is use TableUtils.clearTable(...):
TableUtils.clearTable(header.getUserDao().getConnectionSource(), UserModel.class);
Deleting all of the elements can also be done with the DeleteBuilder:
helper.getUserDao().deleteBuilder().delete();
In realm we have the option to find the first element in the database using findFirst(). But I want to do the reverse of it. I want to last element based on the some condition which I provide.
To find out the first element in the realm database:
GamesDetail learnGameDetail = realm.where(GamesDetail.class).equalTo("gameType", getResources().getString(R.string.learn_row)).notEqualTo("currentBadge",0).findFirst();
In the above query all the rows will have currentBadge as zero. But the last updated row will have currentBadge with some value. I need to find the last updated row with currentBadge not equal to zero.
Instead of
GamesDetail learnGameDetail = realm.where(GamesDetail.class)
.equalTo("gameType", getResources().getString(R.string.learn_row))
.notEqualTo("currentBadge",0)
.findFirst();
You can do
GamesDetail learnGameDetail = realm.where(GamesDetail.class)
.equalTo("gameType", getResources().getString(R.string.learn_row))
.notEqualTo("currentBadge",0)
.findAll()
.last(null);
Please note that insertion order is not kept after deletion. Using findAllSorted() is generally advised.
I'm using Ormlite for database operations on my Android App.
In one of the methods, I need to delete the last row in a table. However I m not able to find out how. I'm successfully deleting rows when giving an argument, but in this specific case I don't have arguments.
Here's my attempt:
LottoDatabaseHelper helper = OpenHelperManager.getHelper(getApplicationContext(), DatabaseHelper.class);
//You get helper
Dao dao = helper.getDao(MyTable.class);
//get your Dao
DeleteBuilder<MyTable, Integer> deleteBuilder = dao.deleteBuilder();
//How can I specify last row here?
deleteBuilder.delete();
Thank you for your help
I haven't used Ormlite .. here is a simple solution which can use with any database ... As you said you can delete a row by passing argument...
try this..
1. Find the last id using the query..
eg. SELECT id FROM table_name ORDER BY id DESC LIMIT 1
// will return the last row id
2. then pass the id as a parameter to delete ...
I use ActiveAndroid to save my objects to the database, it works mostly well. In my application, I use the following scenario:
I save a new object to a table in my database
I select some objects from that table
I add them to a List<>
I delete everything from that table
I use foreach on my List and call 'save' on each object
And here comes the problem. In my table the objects are saved except the aforementioned most recently saved one. I created a counter to check, how many 'save' was called: the counter is 1 more than the count of the objects in the table. I debugged it, no exception was raised, the save was called. I use the latest version of ActiveAndroid (3.0.99)
Any ideas what I should check?
Well, the problem can be seen in the scenario if your read it through.
I copy an existing object to the memory and try to reinsert it. The ORM checks only the mID of the object and if it is not null, it calls an update. As my object had an id, it was tried to be updated though the table was truncated so nothing was updated.
I don't know if it is intentional that the model never checks the table just its own id but it can lead to issues like this.
I have a relation: an order contains many order-items. I'd like to insert it a single statement rather than: inserting order, obtaining generated order id, setting order-id for every order-item and finally inserting order-items.
For instance:
Order newOrder = new Order();
o.setItems(orderItems);
o.insert();
instead of:
Order newOrder = new Order();
newOrder.insert();
foreach orderItems : orderItem.setOrderId(newOrder.getId());
orderItems.insert();
Thanks.
greenDAO does not support this.
Updating lists of related entities is not trivial because items could be added, updated, or deleted. greenDAO is not Hibernate, so you need to do those things manually.