1

I am trying to retrieve a list of dates(in string format) from a PersonDOB column in database. Then I am encountering Database locked issue. I have declared open() and close() methods for the database. Even though I am getting these errors. I am unable to understand, why I am getting this error. I have read some articles like

Question - 1

Question - 2

Not only these, I have tried solutions given in other questions and articles also. But I am unable to get an appropriate solution, that suits my context. In my entire code, I am using the write operation only once. So, there is no problem of data corruption. Here is the code, where I am getting data from the database.

public String[] getDataInArray() { // get data for list and return in array form
        // TODO Auto-generated method stub
        SQLiteDatabase myDB;
        String[] return_columns;
         try {

              myDB=this.openDataBase();       
              String DOB = "PersonDOB";
            String[] columms = new String[]{ DOB };



            Cursor c = myDB.query("Persons", columms, null, null, null, null, null);

            int iDOB = c.getColumnIndex(DOB);

            int rowcount = 0;
              return_columns = new String[c.getCount()];
            for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
            {
                return_columns[rowcount] = c.getString(iDOB);
                rowcount = rowcount + 1;
            }



                  if(c != null)
                 {
                     myDB.close();
                     c.close();
                  }          


                  }catch(SQLException sqle){

                  throw sqle;

                  }
         for (int i = 0;i<return_columns.length;i++)
            {
                                     Log.v("DOB", return_columns[i]); //I am not
 getting this log message in the logcat.
            }
        return return_columns;
        }

And I get the following errors in the logcat -

09-19 15:58:00.140: W/System.err(7115): java.lang.NullPointerException
09-19 15:58:00.140: W/System.err(7115):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
09-19 15:58:00.140: W/System.err(7115):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
09-19 15:58:00.200: W/System.err(7115):     at com.example.fromstart.adapter.open(adapter.java:30)
09-19 15:58:00.210: W/System.err(7115):     at com.example.fromstart.adapter.getDataInArray(adapter.java:222)

Thanks in advance.

Community
  • 1
  • 1
nki
  • 192
  • 3
  • 17

1 Answers1

1

Closing database myDB.close(); is not enough to avoid locks. You need to close the cursors which you are using for database operations before closing the database or when the operation with the cursor is completed. If you are using Threads please make sure they also must be properly closed.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • Thank you for the reply @Avadhani Y, I have now closed the cursor also. Please see my updated code. But I am getting the same error in the log. – nki Sep 19 '13 at 10:46
  • @nki how can u close cursor after database is closed? Place `c.close()` before `myDB.close()` – Avadhani Y Sep 19 '13 at 13:09
  • I have made the change as you said. But also getting the same error. I have placed `c.close();` before `myDB.close();`. That worked for me at that time. – nki Sep 19 '13 at 13:24
  • Do you know, what might be the other reasons, to get this error? – nki Sep 19 '13 at 13:37
  • The chances would be not properly closing the `threads`, `cursors`, `database objects`,... Please refer this document: http://sqlite.org/lockingv3.html – Avadhani Y Sep 19 '13 at 14:48