0

I am working with a database with 4 columns (_id, name, scores, status), and using the following to import all rows into an arrayList.

   Cursor assessmentStatus = myDbHelper.getAllTests();

      assessmentStatus.moveToFirst();

      while (!assessmentStatus.isAfterLast()) {

          allAssessments.add(assessmentStatus.getString(1));

          if(assessmentStatus.getString(3).equals("locked")) { lockedAssessments.add(assessmentStatus.getString(1)); }

          assessmentStatus.moveToNext();
      }

      assessmentStatus.close();

The first statement (allAssessments.add...) works fine. The if statement works if I use getString(0).equals or getString(1).equals, but not if I use getString 3 or 4 - the app crashes. It also won't work if I use getColumnIndex("status").equals

Have tried cleaning the project, but can't figure out this behaviour. Any ideas why this is happening?!

UPDATE:

1) getAllTests is as follows (have also tried rawQuery SELECT * FROM ... etc

    public Cursor getAllTests() 
{
    return myDataBase.query("assessment_tests", new String[] {"_id", "name", "scores", "status"}, null, null, null, null, null);
}

2) All fields are text fields. Uninstalling, cleaning the project and reinstalling has no effect.

3) The database is opened with:

          DataBaseHelper myDbHelper = new DataBaseHelper(null);
      myDbHelper = new DataBaseHelper(this);

      try {
        myDbHelper.openDataBase();
      }catch(SQLException sqle){
        throw sqle;
      }

I know the database creation works fine, as I have other tables which are working perfectly...

cg5572
  • 21
  • 1
  • 4
  • Can you please add the stacktrace with the error message? – micha Aug 25 '12 at 17:36
  • can you also post the code related to database creation and getAllTests function – nandeesh Aug 25 '12 at 17:47
  • Strangely, I can access the value fine when showing it as a toast: `Toast.makeText(getApplicationContext(), assessmentStatus.getString(3), Toast.LENGTH_SHORT).show();` But comparison crashes the application: `if(assessmentStatus.getString(3).equals("locked")) { }` – cg5572 Aug 26 '12 at 13:36

1 Answers1

0

verify if the database field is type text. uninstall the app from the device and run again to force the db to update, if that is the case. You may try to pull out the database if your device is root. the db is (if default) on /data/data/com.yourapp.name/databases/dbname you may pull the db using the "adb pull /data/data/com.yourapp.name/databases/dbname"

Ali
  • 9,800
  • 19
  • 72
  • 152
soynerdito
  • 501
  • 3
  • 10