2

Hi im strugeling with SQLite in android.

Im trying to delete old posts from the db by doing this

this.db.delete(
  EVENT_TABLE_NAME,
  "date < ?", 
  new String[] {String.valueOf(limit.getTime())}
);

Where limit is the current date taken from the Calendar instances and deducted 216000000 milliseconds.

But this seems to remove all posts.

4 Answers 4

3

try this

this.db.delete(
  EVENT_TABLE_NAME,
  "date < "+limit.getTime(),null);
Sign up to request clarification or add additional context in comments.

Comments

0

My guess would be:

context.deleteDatabase(DATABASE_NAME);

1 Comment

No, thats absolutely not what Im looking for. I want to delete a post. Not the entire db
0

You can try something like this:

public static String getDateStringFromDate(Date date, String pattern,
                                           String timezone) {
    // Set locale to US to prevent date issues
    if (!Locale.getDefault().equals(Locale.US)) {
        Locale.setDefault(Locale.US);
    }
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    if (timezone != null) {
        df.setTimeZone(TimeZone.getTimeZone(timezone));
    }
    if (date == null) {
        return null;
    }
    try {
        return df.format(date);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

This function will create a string with your custom date format and you ca create a functional query :) And after that you can use the delete function like this:

this.db.delete(
    EVENT_TABLE_NAME,
    "date < ?", 
    new String[] {getDateStringFromDate(date,pattern,timezone)}

);

Comments

0

This is working for me :

    public static boolean deleteData(String tableName,String where, String[] value){
    sqliteDb = instance.getWritableDatabase();
    sqliteDb.delete(tableName, where, value);
    return true;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.