Search code examples
androidsqliteandroid-logcatandroid-cursoradapter

How to break cursor processing while cursor object hold first row and move to fetch second row?


As I'm fetching only one column from SqLite database but getting more than 1MB of data in my cursor object and I can't split my database query. Is it possible to break cursor processing as soon as cursor fetches first row and at that particular time I want to hold this cursor object values in another object. After that cursor clear this value and move to next for fetching second row in database, this continues till the end of records ?


Solution

  • What if you do the following? (This is just an idea) Fetch all rows you need with the id column only (fetch the id instead of the blob column). Iterate throw that cursor and for each line fetch only one row for the given id with your blob. Then you close that Cursor and you open a new one for the next id row:

    //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();