Delete Records From Sqlite In Android
Solution 1:
Since you are not going to keep any records offline after pushing to server.
you can just delete all the records of the table after the successful API call.
publicvoiddeleteAll(String tableName){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM "+tableName);
db.close();
}
if you plan to keep records offline you can have a separate column like status
to reflect various states of the record(It depends upon the requirement)
Value -> Meaning
A -> Added and not synced to server
S -> Synced to server
M -> Modified the synced record
DL -> Deleted locally
D -> completely deleted both in server and app.
you can use Update
sql command for modifying status of existing column.
If you plan to delete some records based on Status
value.Let's D is completely useless.so we can go ahead like.
publicvoiddeleteMarked(String tableName){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM "+tableName+" where status='D'");
db.close();
}
Solution 2:
You can use following code to delete the records from database which are already sync. But first you have to manage the sync flag in your db, hope you are already doing that which can be done in this way: create one integer or boolean column in your db which tells you that your db is sync with server and after updating this you have to call following method to delete the record which are already sync:
publicbooleandeleteSyncRecord(String tableName)
{
String whereClause = "_isSync" + "=?";
String[] whereArgs = newString[] { "true" };
return db.delete(tableName, whereClause, whereArgs) > 0;
}
Post a Comment for "Delete Records From Sqlite In Android"