lundi 28 décembre 2015

SQLite DELETE doesn't work

I retrieve some events from websites then record them into a database.
After updating my database, I delete the old events, but it doesn't work properly.
It deletes all, not old events.

This is my getAllEvents() method

public List<Event> getAllEvents() {
    i=0;
    Log.e(TAG,"db handler getallevents");
    List<Event> eventList = new ArrayList<>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_EVENTS+" ORDER BY "+ KEY_TYPE +" ASC";

     db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
  //  Log.e(TAG,"cursor: "+cursor.getColumnNames());
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {i++;
            Event event = new Event();
           // event.setID(Integer.parseInt(cursor.getString(0)));
            event.setName(cursor.getString(1));

            event.setDescription(cursor.getString(7));
            event.setLink(cursor.getString(4));
            event.setLocation(cursor.getString(2));
            event.setType(cursor.getString(3));
            event.setStartDate(cursor.getString(5));
            event.setEndDate(cursor.getString(6));

            // Adding contact to list
           eventList.add(event); Log.e(TAG,i+"\n");
           event.writeSc(event);

        } while (cursor.moveToNext());


    }
    currentDate = Calendar.getInstance();
    formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm");//for sqlite

    Log.v(TAG,"db handler getallevents silinenler");
    String sql = "DELETE FROM "+TABLE_EVENTS+" WHERE "+KEY_END_TIME+" < '"
            + formatter.format(currentDate.getTime())+"'"
            +" OR "+KEY_END_TIME+" < '"
            + formatter.format(currentDate.getTime()).substring(0,10)+"'";
    db = this.getWritableDatabase();
    cursor = db.rawQuery(sql, null);
    //  Log.e(TAG,"cursor: "+cursor.getColumnNames());
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {i++;
            Event event = new Event();
            // event.setID(Integer.parseInt(cursor.getString(0)));
            event.setName(cursor.getString(1));

            event.setDescription(cursor.getString(7));
            event.setLink(cursor.getString(4));
            event.setLocation(cursor.getString(2));
            event.setType(cursor.getString(3));
            event.setStartDate(cursor.getString(5));
            event.setEndDate(cursor.getString(6));

            // Adding contact to list
            eventList.add(event); Log.e(TAG,i+"\n");
            event.writeSc(event);

        } while (cursor.moveToNext());}

    //deleteOldEvents();;

    db.close();
    Log.e(TAG,"db handler getallevents sonu");
    return eventList;
}

This is deleteOldEvents()

public void deleteOldEvents(){
    Log.e(TAG,"db handler deleteOldEvents");
    Log.e(    "Chico","db handler deleteOldEvents");
    currentDate = Calendar.getInstance();
    formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm");//for sqlite
    String sql = "DELETE FROM "+TABLE_EVENTS+" WHERE "+KEY_END_TIME+" < '"
            + formatter.format(currentDate.getTime())+"'"
            +" OR "+KEY_END_TIME+" < '"
           + formatter.format(currentDate.getTime()).substring(0,10)+"'";


    currentDate = Calendar.getInstance();


   // List<Event> eventList = new ArrayList<Event>();
    // Select All Query
    //  String selectQuery = "SELECT  * FROM " + TABLE_EVENTS+" WHERE "+KEY_START_TIME+
    //  " BETWEEN ('2015-12-01 00:00') AND ('2015-12-29 23:59')" + " AND "+KEY_TYPE+" = \""+type_+"\"";

    //su anki saatten itibarenleri aliyorum


    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(sql, null);
    if (cursor.moveToFirst()) {
        do {
            Event event = new Event();
            // event.setID(Integer.parseInt(cursor.getString(0)));
            event.setName(cursor.getString(1));

            event.setDescription(cursor.getString(7));
            event.setLink(cursor.getString(4));
            event.setLocation(cursor.getString(2));
            event.setType(cursor.getString(3));
            event.setStartDate(cursor.getString(5));
            event.setEndDate(cursor.getString(6));
            Log.e(TAG,"silinen tarih: ");

            event.writeSc(event);

        } while (cursor.moveToNext());
    }
getAllEvents();
    Log.e(TAG,"db handler delete events sonu");

}

This is my addEvents() method (I record with this one):

public void addEvents(List<Event> events) {
    Log.e(TAG,"db handler addevent");
    SQLiteDatabase db = this.getWritableDatabase();

    int kk=0;

    for(Event event:events) { Log.v(TAG, String.valueOf(kk)+" events dizi `size "+events.size());kk++;`
        //database ekleme için
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, event.getName()); // event Name
    values.put(KEY_LOCATION, event.getLocation());
    values.put(KEY_TYPE, event.getType());
    values.put(KEY_LINK, event.getLink());
    values.put(KEY_START_TIME, event.getStartDate()); // Event start
    values.put(KEY_END_TIME, event.getEndDate());
    values.put(KEY_DESCRIPTION, event.getDescription());
        Log.d(TAG,"db handler addevent vales: "+values.toString());

    // Inserting Rows
    db.insert(TABLE_EVENTS, null, values);
    }

    //setLastUpdateTime();
    db.close(); // Closing database connection
    Log.e(TAG,"db handler addevent sonu");
}

These are inside the dbHandler class.
I retrieve the data in the AsyncTask class, in its postExecute() method:

dbhandler = new DBHandler(mActivity);

        dbhandler.addEvents(events);

    // dbhandler.deleteOldEvents();

   dbhandler.getAllEvents();

This line

dbhandler.deleteOldEvents();

was in the postExecute() method, but I deleted it.
It was not working correctly.

Should I put that invoking inside addevents() method while recording to database or put that method all delete method?

Aucun commentaire:

Enregistrer un commentaire