【问题标题】:SQLite CursorWindow limit - How to avoid crashSQLite CursorWindow 限制 - 如何避免崩溃
【发布时间】:2017-08-14 15:12:38
【问题描述】:

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

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;
}

在正常情况下,此函数可以正常工作,但在某些情况下,此函数会产生异常:

Window is full: requested allocation 3209815 bytes, free space 2096647 bytes, window size 2097152 bytes

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

如何解决我的问题?

【问题讨论】:

    标签: android sqlite android-cursor


    【解决方案1】:

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

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

    这样,

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      相关资源
      最近更新 更多