dimanche 26 juillet 2015

how to delete rows from sqlite by comparing date

i want to create a delete function in my app to delete the rows added more than a day ago and a column whose has a value 'C'. I'm having problem how to compare the current system date with date stored in the database column,

public int RemoveSyncData(){

  DBHelper = new DatabaseHelper(context); 
  DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
  Calendar calObj = Calendar.getInstance();
  String currentDate = dateFormat.format(calObj.getTime());
  Log.e("current date",currentDate);

  int totalRows = db.delete(DATABASE_TABLE_ATTENDANCE,SYN_STATUS + "= ? and " + CREATED_DATE + " < " + currentDate, new String[] {"C,"});

  db.close();
  return totalRows;
}

I'm using the same date format for comparing, bleow is my Logcat

07-24 10:50:37.018: E/AndroidRuntime(21580): Caused by:   android.database.sqlite.SQLiteException: near "10": syntax error (code 1): ,    while compiling: DELETE FROM dailyattendance WHERE sync_status= ? and   created_date < 07-24-2015 10:50:36 AM
07-24 10:50:37.018: E/AndroidRuntime(21580):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-24 10:50:37.018: E/AndroidRuntime(21580):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-24 10:50:37.018: E/AndroidRuntime(21580):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-24 10:50:37.018: E/AndroidRuntime(21580):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

Aucun commentaire:

Enregistrer un commentaire