I have trouble putting together a delete statement in my Android application (I am using OrmLite).
I have a table filled with records. Two of the fields are “dateCreated” (type Date) and “imageSize” (type int). In my code I have a method free(int size). This method tells me that I have to delete oldest records from my table that sum “imageSize” <= size.
For instance .. i get parameter 1000. Each record has value lets say 100. That means i have to delete 10 of the oldest records.
Can some one please provide me with optimal raw SQL statement or even better an OrmLite code for this?
I would be most gratefull.
Unfortunately, you can’t do this with a single SQL statement. There is no way to say
You could doing multiple queries until you found the oldest records whose sum is less than X but it would take a number of separate SQL calls.
I’d recommend selecting the last X images with their sizes and then doing a delete of the right number of images using a
DELETE ... IN .... Here’s the pseudo code: