如何在光标对象保持第一行并移动以获取第二行时中断游标处理?

时间:2012-09-25 19:14:40

标签: android sqlite android-logcat android-cursoradapter

由于我只从SqLite数据库中获取一列但在游标对象中获取的数据超过1MB而我无法拆分数据库查询。一旦游标获取第一行就可以中断游标处理,并且在该特定时间我想将该游标对象值保存在另一个对象中。在该游标之后清除此值并移至next以获取数据库中的第二行,这将一直持续到记录结束?

2 个答案:

答案 0 :(得分:1)

如果您正在使用Cursor(SQLiteCursor) - 在获取第一行后,无法阻止光标“吃掉内存”(如您所说的中断处理)。

android.database.sqlite是sqlite3库的java包装器,用C编写 事实是sqlite3没有计算记录语句将产生多少的函数,因此你必须在sqlite3_step函数的帮助下扫描整个结果集,直到它返回SQLITE3_DONE
SQLiteCursor源自CursorWindow。 目前CursorWindow Cursors方法首次调用getCount()(有一些本机方法) - 它执行两项操作:计算行数并缓存这些行。

custom port(russian)的sqlite3 for android,你需要的功能。
如果你不懂俄语:
java code
native code
native sources

答案 1 :(得分:1)

如果您执行以下操作该怎么办? (这只是一个想法) 仅使用id列获取所需的所有行(获取id而不是blob列)。 迭代抛出光标,并为每个行仅使用blob获取给定id的一行。然后关闭该Cursor,然后为下一个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();