Skip to content Skip to sidebar Skip to footer

Cursorindexoutofboundsexception When Accessing Sqlite Database

Hi everyone I’ve been having some difficulty, hope one of you can help. I’m trying to create a little program to store my timetable for college. I’ve been having some trouble

Solution 1:

If you got through the documentation of getReadableDatabase, it says

Like getWritableDatabase(), this method may take a long time to return, so you should not call it from the application main thread, including from ContentProvider.onCreate()

And what I see in your program, first you are calling this in constructor of DBAdapter

this.DBHelper = new DatabaseHelper(this.context);
this.DBHelper.getWritableDatabase();
this.DBHelper.getReadableDatabase();

and then in onCreate you are calling db.open(); where you are again calling this.DBHelper.getWritableDatabase();

So, it might be possible yours database is not properly loaded. And try to do all this operation in background thread ex AsyncTask.

Hope this help!!!

Solution 2:

I don't see where you ever actually call the "close()" function to close the database when you are finished with it. Maybe try to close it after you are done adding your sections. I see you have provided methods for closing it but it doesn't look like you ever actually used them.

From the docs:

Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close() when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed. Database upgrade may take a long time, you should not call this method from the application main thread, including from ContentProvider.onCreate().

Post a Comment for "Cursorindexoutofboundsexception When Accessing Sqlite Database"