user485175
user485175

Reputation: 39

Android SQL Delete row not working

Firstly I am creating a Database in a helper class as follows:

public void onCreate(SQLiteDatabase db) {
    String sql = "create table " + TABLE + "( " + ID
            + " integer primary key autoincrement, " + FIELD1 + " text, "
            + FIELD2 + " text);";
    Log.d("EventsData", "onCreate: " + sql);
    db.execSQL(sql);
}// Where public static final String ID = "_id"; ect.

I have then inserted data and I have seen it works by displaying the inserted data in a TextView. My problems come when I need to delete a row.

I can deleted everything by using

db.delete(TABLE, null , null); (again I can see that this works)

However if I change it to deleted a single row such as

db.delete(TABLE, "_id" + Index, null); Where for example int Index =4;

nothing happens, I get no errors and no delete.

Can anyone help with why this happens?

Upvotes: 2

Views: 1150

Answers (3)

Aplit
Aplit

Reputation: 71

db.delete(TABLE, "_id=" + Index, null);

Upvotes: 0

Sajidkhan
Sajidkhan

Reputation: 618

SQLiteDatabase db = this.getWritableDatabase();    
db.delete(TABLE_CONTACT, _ID + "=" + _id, null);
db.close();

Upvotes: 2

antlersoft
antlersoft

Reputation: 14751

Try

db.delete(TABLE, "_id = ?", new String[] { "" + Index });

Upvotes: 1

Related Questions