我正在阅读一个包含32000个条目的数据库,并按照以下方式更新它们:
public void updateTable(@NotNull String title) {
Pattern pattern = Pattern.compile("<span id=\"v(.+?)\" class=\"v\">.+?</span>");
SQLiteDatabase database = dbwraper.getDatabase();
Matcher m = pattern.matcher("");
ContentValues values = new ContentValues();
String[] args = new String[]{""};
String selection = "_Id=?";
HashMap<String, Integer> map = new HashMap<>();
try (SQLiteCursor cursor = (SQLiteCursor) database.query(TABLE_NAME,
new String[]{"_Id", "Label", "Content", "AdjustmentInfo"}, null, null, null, null, "_Id ASC")) {
cursor.moveToFirst();
do {
byte[] info = cursor.getBlob(3);
args[0] = cursor.getString(0);
if (info == null) {
database.delete(TABLE_NAME, selection, args);
continue;
}
String content = process(dbwraper.getDeobfuscator(), cursor.getBlob(2));
m.reset(content);
m.matches();
log.e("m.group(1) = " + m.group(1));
String[] ids = m.group(1).split("-");
int id = Integer.parseInt(ids[0]) * 1000000 + Integer.parseInt(ids[1]) * 1000 + Integer.parseInt(ids[2]);
map.put(args[0], id);
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
}
database.beginTransaction();
for (String key : map.keySet()) {
args[0] = key;
values.clear();
values.put("_Id", map.get(key));
database.update(TABLE_NAME, values, selection, args);
}
database.setTransactionSuccessful();
database.endTransaction();
}
当光标读取最后一个条目时,问题会出现问题。我收到这个错误:
CursorWindow: Failed to read row 1411, column 3 from a CursorWindow which has 1411 rows, 4 columns.
我似乎无法看到问题。这是内存分配问题吗?或者我的数据库格式不正确?
答案 0 :(得分:2)
只需更改您的循环:
cursor.moveToFirst();
do {
[...]
} while (cursor.moveToNext());
到
// IMPORTANT: REMOVE THIS LINE!! cursor.moveToFirst();
while (cursor.moveToNext() {
[...]
}