SQLite CursorWindow 限制 - 如何避免崩溃

Mat*_*tia 4 sqlite android android-cursor

我必须执行查询并将结果存储在列表中,我使用的函数如下:

List<SpoolInDB> getSpoolInRecords(String label, boolean getLastInserted) {
    List<SpoolInDB> spoolInList = new ArrayList<>();
    try {
        if (mdb == null)
            mdb = mdbHelper.getWritableDatabase();

        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(TABLE_SPOOLIN);

        Cursor c = qb.query(mdb, null, " label='" + label + "'", null, null, null, " dateins " + (getLastInserted ? "desc" : "asc"));
        if (c != null) {
            c.moveToFirst();
            if (c.getCount() > 0) {
                int ndxid = c.getColumnIndex("id");
                int ndxserverID = c.getColumnIndex("serverID");
                int ndxlabel = c.getColumnIndex("label");
                int ndxvalue = c.getColumnIndex("value");
                int ndxpriority = c.getColumnIndex("priority");
                int ndxdateins = c.getColumnIndex("dateins");

                do {
                    SpoolInDB spoolIn = new SpoolInDB();
                    spoolIn.setId(c.getString(ndxid));
                    spoolIn.setServerID(c.getString(ndxserverID));
                    spoolIn.setLabel(c.getString(ndxlabel));
                    spoolIn.setValue(c.getString(ndxvalue));
                    spoolIn.setPriority(c.getString(ndxpriority));
                    spoolIn.setDateins(c.getString(ndxdateins));
                    spoolInList.add(spoolIn);

                } while (c.moveToNext());
            }
            c.close();
        }
    } catch (Exception e) {
        wil.WriteFile("4)DbGest - Exception: " + e.toString());
    }
    return spoolInList;
}
Run Code Online (Sandbox Code Playgroud)

在正常情况下,该函数可以完美运行,但在某些情况下,该函数会产生异常:

Window is full: requested allocation 3209815 bytes, free space 2096647 bytes, window size 2097152 bytes
Run Code Online (Sandbox Code Playgroud)

出现这个问题是因为在“值”字段中我可以存储在某些情况下可能大于 2mb 的 json 数据,我无法预测数据何时大于 2mb,我需要一个始终有效的解决方案。

我该如何解决我的问题?

Bob*_*Bob 6

CursorWindow大小限制为 2MB(截至目前)。您无法读取大小超过 2MB 的单行,因为无法将其放入Cursor.

因此,您可以解析它并将其存储在数据库中单独的列或表中,而不是将整个 JSON 存储为单个元素。

以便,

  1. 您可以将 JSON 中不需要的数据保留在数据库中。
  2. 您可以一次查询部分数据(几列),这样查询的数据就不会超过2MB的CursorWindow限制。

或者您可以尝试其他数据库系统,例如 Realm(我还没有尝试过,所以我不确定那里是否有任何限制)。