title: SQLite Query: SQLite Query to delete data, such that only current date data exits in db
body: <p><img src="https://i.stack.imgur.com/qaTZ6.png" alt="database schema" /></p>
  <p>I have a database with following columns(screenshot attached)</p>
  <blockquote>
  <p><strong>_id = INTEGER PRIMARY KEY</strong></p>
  <p><strong>guid = TEXT(unique id of the image)</strong></p>
  <p><strong>pubDate= LONG(time since epoch)</strong></p>
  <p><strong>imgsrc = BLOB(image byte)</strong></p>
  </blockquote>
  <p>The requirement is such that i want to keep only images which are downloaded today. I want to delete stale data...i.e yesterdays data.</p>
  <p>For ex consider <code>id = 1</code> with <code>guid = 471c9744666b6f51</code></p>
  <p>This image was downloaded on <strong>Wed Dec 26 2012 17:11:05</strong>, so database should contain data only for Dec 26 and not Dec 25. Its not necessary that the first pubdate will be have today's date. So, probably first we need to sort the db by pubdate in desc-ding order and then use the first pubdate as an input to delete older data.</p>
  <p>Any help as to how to form a SQL statement will be highly appreciated.</p>
  <blockquote>
  <p>Answer:Ended up doing something like this..</p>
  </blockquote>
  <pre><code>long maxDate = 0;
  
  Cursor cursor = getApplicationContext().getContentResolver().query(IMAGES_URI, null, null, null, PUB_DATE + &quot; DESC &quot;);
  
  if (cursor != null &amp;&amp; cursor.moveToNext()) {
  maxDate = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseHelper.PUB_DATE));
  cursor.close();
  }
  Calendar cal = Calendar.getInstance();
  cal.setTimeInMillis(maxDate);
  cal.add(Calendar.HOUR, -12);
  
  int delcount = getApplicationContext().getContentResolver().delete(IMAGES_URI, PUB_DATE + &quot; &lt;= &quot; + cal.getTimeInMillis(), null);
  </code></pre>