Reputation: 49
I'm developing an android application in which every day i want to check 1 month older records present in table, if i want to delete 1 month older records from table, i m saving insertion time as long value (System.currenttimemillis) in the table its data type is REAL. Is there any direct query available in sq-lite to delete older records by specifying interval.?
I got below query in MySQL is this works in sq-lite?? please help me...
DELETE FROM [table] WHERE [InsertTime] < DATE_SUB(NOW(), INTERVAL 1 MONTH);
Upvotes: 1
Views: 2584
Reputation: 141889
A query like this should do the job:
DELETE FROM <table> WHERE datetime(<dateColumn> / 1000, 'unixepoch') < datetime('now', '-1 month')
The division by 1000
is necessary since you're storing the timestamp in milliseconds, but SQLite understands seconds for unixepoch
.
Upvotes: 3
Reputation: 1790
This subtracts one month from current date.
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
Once u have the date perform delete query
Upvotes: 0