简体   繁体   中英

Error with cursor and SQLite in android

I have an error with a cursor when I'm reading a database:

04-07 18:11:25.672: ERROR/AndroidRuntime(5801): android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=666 (# cursors opened by this proc=666)

It happens on the line

cursor.getCount()

The entire file is 8kb, it has 32 lines, but IDs go from 737 to 768, is the problem from there?

(I have noticed that if ID is below 600, there are no problems)

(Question answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

I solved the problem. My code was:

public News getNewsWithID(int id){
        Cursor c = bdd.query(TABLE_NEWS, new String[] {COL_ID, COL_ASSO, COL_DATE, COL_HEURE, COL_TYPE, COL_TITRE, COL_CONTENU, COL_SERVEURID}, COL_ID +"='"+ id +"'" , null, null, null, null);
        return cursorToNews(c);
    }

I changed to:

public News getNewsWithID(int id){
        Cursor c = bdd.query(TABLE_NEWS, new String[] {COL_ID, COL_ASSO, COL_DATE, COL_HEURE, COL_TYPE, COL_TITRE, COL_CONTENU, COL_SERVEURID}, COL_ID +"='"+ id +"'" , null, null, null, null);
        News temp = cursorToNews(c);
        c.close();
        return temp;
    }

I thought that the cursor was closed on onDestroy().

You should really be closing the cursor in a finally block, to ensure that it's closed even if an exception happens.

public News getNewsWithID(int id){
    Cursor c = bdd.query(TABLE_NEWS, new String[] {COL_ID, COL_ASSO, COL_DATE, COL_HEURE, COL_TYPE, COL_TITRE, COL_CONTENU, COL_SERVEURID}, COL_ID +"='"+ id +"'" , null, null, null, null);
    try {
        return cursorToNews(c);
    } finally {
        c.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM