如何在游标对象按住第一行并移动到获取第二行时中断游标处理



因为我只从 SqLite 数据库中获取一列,但在游标对象中获取超过 1MB 的数据,并且无法拆分数据库查询。是否有可能在光标获取第一行后立即中断游标处理,并且在那个特定时间我想将此游标对象值保存在另一个对象中。在该光标清除此值并移动到下一个以获取数据库中的第二行之后,这一直持续到记录的末尾?

如果执行以下操作怎么办?(这只是一个想法)仅使用 id 列提取所需的所有行(提取 id 而不是 blob 列)。迭代抛出该游标,对于每一行,仅提取具有 blob 的给定 id 的一行。然后关闭该光标,并为下一个 id 行打开一个新光标:

//just fetch the ids of the wanted rows
Cursor idCursor = db.query("TABLE_NAME",new String[]{"_id"}, null, null, null,null,null);
Cursor blobCursor;
    //for each row (id)
    while(idCursor.moveToNext())
    {
        //fetch one row with the blob of the given id
        blobCursor = db.query("TABLE_NAME",new String[]{"image"}, "_id = ?", new String[] {new Long(idCursor.getLong(0)).toString()}, null,null,null);
        if(blobCursor.moveToFirst())
        {
            //get the blob and store it
            blobCursor.getBlob(0); 
        }
        blobCursor.close(); //close the cursor (and release resources)
    }
idCursor.close();

如果您使用的是Cursor(SQLiteCursor) - 没有办法防止光标在获取第一行后"吞噬内存"(如您所说中断处理)。

android.database.sqlite 是一个 sqlite3 库的 java 包装器,它是用 C 语言编写的。
事实是sqlite3没有函数来计算语句将生成多少记录,因此您必须借助sqlite3_step函数扫描整个结果集,直到它返回SQLITE3_DONE
SQLiteCursor派生自CursorWindowCursorWindow(有一些本机方法)目前Cursors getCount()第一次调用该方法 - 它做两件事:计算行计数并缓存这些行。

有适用于Android的sqlite3的自定义端口(俄语),具有您需要的功能。
如果您看不懂俄语:
Java代码
原生代码
原生来源

最新更新