插入和删除信息,SQLite数据库

时间:2016-03-24 07:35:28

标签: android sqlite

我正在使用本教程http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/和作者创建的方法:addContact和deleteContact。我了解如何添加联系人:database.addContact(new Contact("Ravi", "9100000000"));但是如何删除该联系人?有人请帮帮我!

的addContact:

void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

deleteContact:

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

2 个答案:

答案 0 :(得分:-1)

试试!!!

这是删除单个联系人的方法。

public static void delete(int rowID) {
  SQLiteDatabase database = this.getWritableDatabase();
    database.delete(TABLE_ARTICLE, KEY_ID + " =? ", new String[]{String.valueOf(rowID)});
}

这是方法删除所有联系人

 public static void deleteAll() {
    String sql = "DELETE FROM " + YOUR_NAME_TABLE;
    SQLiteDatabase database = this.getWritableDatabase();
    database.execSQL(sql);

}

答案 1 :(得分:-2)

试试这个。

public int DeleteRow() {
//Delete from table where name=Ravi
    SQLiteDatabase db = this.getWritableDatabase();
    String wherearegs[] = {"Ravi"};
    int count = db.delete(TableName, Column_Name + "=?", wherearegs);
    return count;
}