Updating data already in the database

The MicroJobs application enables the user to edit a job by clicking on the job in the Jobs list and choosing the Edit Job menu item. The user can then modify the strings for employer, job title, and description in the editJob form. After the user clicks on the Update button on the form, the following line of code is executed:

db.editJob((long)job_id, employer.id, txtTitle.getText().toString(), txtDescription.getText().toString());

This code calls the editJob method, passing the job ID and the three items the user can change: employer ID, job title, and job description. The editJob method does the actual work of modifying the job in the database.

Example 8-3 shows you how to use the update method.

Example 8-3. Using the update method /**

* Update a job in the database.

* @param job_id The job id of the existing job

* @param employer_id The employer offering the job

* @param title The job title

* @param description The job description */

public void editJob(long job_id, long employer_id, String title, String description) { ContentValues map = new ContentValues(); map.put("employer_id", employer_id); map.put("title", title); map.put("description", description);

String[] whereArgs = new String[]{Long.toString(job_id)};

try{

getWritableDatabase().update("jobs", map, "_id=?", whereArgs);© } catch (SQLException e) {

Log.e("Error writing new job", e.toString());

Here are some of the highlights of the code in Example 8-3:

O The first parameter to update is the name of the table to manipulate. The second is the map of column names to new values. The third is a small snippet of SQL; in this case, it's a SQL template with one parameter. The parameter is marked with a question mark, and is filled out with the contents of the fourth argument.

Example 8-4 shows you how to use the execSQL method.

Example 8-4. Using the execSQL method /**

* Update a job in the database.

* @param job_id The job id of the existing job

* @param employer_id The employer offering the job

* @param title The job title

* @param description The job description */

public void editJob(long job_id, long employer_id, String title, String description) { String sql =

"UPDATE jobs " + "SET employer_id = ?, "+ " title = ?, "+ " description = ? "+ "WHERE _id = ? ";

Object[] bindArgs = new Object[]{employer_id, title, description, job_id}; try{

getWritableDatabase().execSQL(sql, bindArgs); } catch (SQLException e) {

Log.e("Error writing new job", e.toString());

For the application in Example 8-4, we show the simplest possible function. This makes it easy to understand in a book, but is not enough for a real application. In a real application, you would want to check input strings for invalid characters, verify that the job exists before trying to update it, verify that the employer_id value is valid before using it, do a better job of catching errors, etc. You would also probably authenticate the user for any application that is shared by multiple people.

0 0

Post a comment