How to avoid db not close and cursor exception

前端 未结 2 923
粉色の甜心
粉色の甜心 2021-01-07 06:23
02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT * FROM 
02         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 06:45

    You should reverse the close statements within your onDestroy() method. First close the cursor, then the db:

    if (cursor!=null){
        cursor.close();
    }
    if (db!=null){
        db.close();
    }
    

    You basically need to reverse the order of creating/opening db and cursor.

    Also note that the db/cursor in the example is opened in onCreate() which is a callback from the system. You may want to close the cursor/db before leaving that method. You are free to cache the DataSQLHelper in your application.

    In my application Zwitscher, I have put the whole db/cursor handling with the methods of the SQLHelper class so that the layers above do not have to care about it. See its TweetDB class.

提交回复
热议问题