Android Tutorial

(avery) #1

By : Ketan Bhimani


306 

Because we are not updating the other fields, we do not need to
include them in the ContentValues object. We include only the title
field because it is the only field we change.

Deleting Records

You can remove records from the database using the remove()
method. The remove() method takes three arguments:

 The table to delete the record from
 An optional WHERE clause, in which? identifies a WHERE clause
argument
 An array of WHERE clause arguments, each of which is substituted in
place of the ?’s from the second parameter

Passing null to the WHERE clause deletes all records within the
table. For example, this function call deletes all records within the
table called tbl_authors:

mDatabase.delete(“tbl_authors”, null, null);


Most of the time, though, we want to delete individual records by
their unique identifiers. The following function takes a parameter
bookId and deletes the record corresponding to that unique id
(primary key) within the table called tbl_books:

public void deleteBook(Integer bookId) {
mDatabase.delete(“tbl_books”, “id=?”,
new String[] { bookId.toString() });
}


You need not use the primary key (id) to delete records; the
WHERE clause is entirely up to you. For instance, the following
function deletes all book records in the table tbl_books for a given
author by the author’s unique id:
Free download pdf