2
例如:它是安全從Android的SQLite數據庫中刪除行,而迭代光標
public void removeStaleMovies(Set<String> updatedMovieList) {
Cursor cur = this.getReadableDatabase().rawQuery("SELECT id, title, year FROM movie", null);
cur.moveToFirst();
while (!cur.isAfterLast()) {
String title = cur.getString(1);
String year = cur.getString(2);
if (!updatedMovieList.contains(title + "-" + year)) {
// delete the row where 'id' = cur.getString(0)
// OR, delete the row using the object at the cursor's current position, if that's possible
// OR, if deletion isn't safe while iterating, build up a list of row id's and run a DELETE statement after iteration is finished
}
}
}
是缺失的安全呢?或者它會導致一些不可預知的行爲?我知道this similar question,但我仍然不確定。
我想你試圖通過ID刪除電影?要麼?你試圖刪除哪些電影? –
應該刪除數據庫中不在提供的HashSet中的任何電影 – RTF
是的,它是安全的。但是,如果您可以執行刪除語句來一次性刪除該行,則更好。 – ramaral