删除SQLite数据库中的条目

时间:2013-12-11 12:48:33

标签: android eclipse sqlite

我刚接触到android编程,也是SQLIte.Iam从site.Iam编写联系人的代码。如果我想删除一些ID为10的联系人,我应该写什么?

public void deleteContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
    db.close();
}

3 个答案:

答案 0 :(得分:1)

您可以检查delete返回以记录已删除的行数:

public void deleteContact(Contact contact) {
    int n;
    SQLiteDatabase db = this.getWritableDatabase();
    n=db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] {String.valueOf(contact.getID())});
    db.close();
    Log.v("deleteContact", "Deleted "+String.valueOf(n)+" row(s).");
}

答案 1 :(得分:0)

要匹配整数,您不能传递字符串参数,请改用以下代码:

public void deleteContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + "=" + contact.getID(), null);
    db.close();
}

答案 2 :(得分:0)

如果要确保删除该行,请使用:

Cursor cursor = this.dB.query(Table_name,new String[] {"id","column2"}, "id LIKE '" + row_Id + "%'" ,  null , null , null , null );

if (cursor.moveToFirst()) {
System.out.print("Still Exists");
}
else
{
System.out.print("Deleted");
}