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.

4

1 回答 1

1

关闭数据库myDB.close();不足以避免锁定。在关闭数据库之前或使用游标的操作完成时,您需要关闭cursors您正在使用的操作。database如果您使用Threads,请确保它们也必须正确关闭。

于 2013-09-19T10:40:52.320 回答