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.
try this
this.db.delete(
EVENT_TABLE_NAME,
"date < "+limit.getTime(),null);
My guess would be:
context.deleteDatabase(DATABASE_NAME);
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)}
);
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;
}
精彩评论