Tobi Adeyinka Tobi Adeyinka - 14 days ago 6
Android Question

Cursor returns with no result after SQLite query (passes unit test but fails at run time) -- android

i have this method:

public Note getNoteById(long id) {
Cursor cursor = database.query(Config.NOTES_TABLE, Config.NOTES_COLUMNS, Config.ROW_ID+"="+id, null, null, null, Config.ROW_ID + " DESC");

//no match found
if(cursor.getCount() == 0)
return null;

cursor.moveToFirst();
Note result = new Note();
result.setId(cursor.getLong(cursor.getColumnIndex(Config.ROW_ID)));
result.setTitle(cursor.getString(cursor.getColumnIndex(Config.TITLE)));
result.setText(cursor.getString(cursor.getColumnIndex(Config.TEXT)));
result.setViewOrder(cursor.getLong(cursor.getColumnIndex(Config.VIEW_ORDER)));
result.setCreated(cursor.getString(cursor.getColumnIndex(Config.CREATED)));
result.createTagsFromString(cursor.getString(cursor.getColumnIndex(Config.TAGS)));

return result;
}


My problem is in the first few lines. The cursor at the beginning always returns with a count of 0 therefore making the function always returns null -- ONLY AT RUNTIME! -- i have written a test case for this method and it passes so i cant figure out why it would only fail at runtime?

heres the relevant part of the test case. i basically insert new data to the table and try to get it back by its id and compare attributes to confirm its the same:

@Test
public void testGetNoteByIdMatch(){
testTable.load();

Assert.assertEquals(0, testTable.getAllNotes().getCount());

testTable.newEntry(note1);
testTable.newEntry(note2);

Cursor cursor = testTable.getAllNotes();
Assert.assertEquals(2, testTable.getAllNotes().getCount());
cursor.moveToFirst();

Note result = new Note();
result.setId(cursor.getLong(cursor.getColumnIndex(Config.ROW_ID)));
result.setTitle(cursor.getString(cursor.getColumnIndex(Config.TITLE)));
result.setText(cursor.getString(cursor.getColumnIndex(Config.TEXT)));

Note checker = testTable.getNoteById(result.getId());
Assert.assertEquals(result.getId(), checker.getId());
Assert.assertEquals(result.getTitle(), checker.getTitle());

//move and try another search
cursor.moveToNext();
result = new Note();
result.setId(cursor.getLong(cursor.getColumnIndex(Config.ROW_ID)));
result.setTitle(cursor.getString(cursor.getColumnIndex(Config.TITLE)));
result.setText(cursor.getString(cursor.getColumnIndex(Config.TEXT)));

checker = testTable.getNoteById(result.getId());
Assert.assertEquals(result.getId(), checker.getId());
Assert.assertEquals(result.getTitle(), checker.getTitle());

}


this is the table structure in the onCreate method for the database:

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + Config.NOTES_TABLE + " (" +
Config.ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
Config.TITLE + " TEXT NOT NULL, " +
Config.TEXT + " TEXT NOT NULL, " +
Config.CREATED + " TEXT NOT NULL, " +
Config.VIEW_ORDER + " INTEGER NOT NULL, " +
Config.TAGS + " TEXT NOT NULL)"
);

}

Answer

Your test checks that the values can be read out in the same order they were inserted, but does not actually verify the data is going in as expected. It's possible that the result and checker objects both have null values for all properties. Verify that the object properties have the expected values once read out of the db.