Посмотрите на примеры 8-3 и 8-4 здесь .
Пример 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