看示例 8-3 和 8-4here http://androidapps.org.ua/i_sect11_d1e7013.html.
例8-3。使用更新方法
/**
* 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());
}
}
以下是例 8-3 中代码的一些要点:
例 8-4 展示了如何使用 execSQL 方法。
例8-4。使用 execSQL 方法
/**
* 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());
}
}
该消息要求您使参数使用 sql 变量而不是 sql 文字。
每个 sql 查询都会被解析、生成计划并存储在 sql 语句缓存中。
具有相同文本的查询将从缓存中获取。
--One query
SELECT * FROM Customers WHERE Id = @1 (@1 = 3)
SELECT * FROM Customers WHERE Id = @1 (@1 = 4)
SELECT * FROM Customers WHERE Id = @1 (@1 = 5)
具有不同文本(包括文字)的查询无法在缓存中找到,并且会(无用地)添加到缓存中。
--Three Queries.
SELECT * FROM Customers WHERE Id = 3
SELECT * FROM Customers WHERE Id = 4
SELECT * FROM Customers WHERE Id = 5