I use a string comparison on the date format to delete records on my database. I want to delete the past 5 minutes data. Here's the code that I am using. It seems to be not working. It does not delete any records. Why ?
int rangeInMinutes = -5;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar now = Calendar.getInstance();
Calendar range = Calendar.getInstance();
range.add(Calendar.MINUTE, rangeInMinutes);
queryString.append(range);
StringBuilder queryString = new StringBuilder("date("'"+now +"'") > date("'"+range+"'");
int c = mydatabase.delete("mytable", queryString.toString(), null)
The logcat shows: 0 record is deleted...
- I store the dates in a String format.
UPDATE:
After converting the date field from Text to Long in SQLite.
myddatabase.delete("mytable", "date > datetime('now','-5 minutes') AND date < datetime('now') ", null);
Still It does not delete any records. Why ?
I store the dates in a String format.Why not use the system unit of milliseconds and store as integer? It will make comparisons much easier.