SQLite 查询后游标 returns 没有结果(通过单元测试但在 运行 次失败)-- android
Cursor returns with no result after SQLite query (passes unit test but fails at run time) -- android
我有这个方法:
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;
}
我的问题出在前几行。开头的游标总是 returns,计数为 0,因此使函数总是 returns null——仅在运行时! -- 我已经为这个方法编写了一个测试用例并且它通过了所以我无法弄清楚为什么它只会在运行时失败?
这里是测试用例的相关部分。我基本上将新数据插入 table 并尝试通过其 id 取回它并比较属性以确认其相同:
@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());
}
这是数据库 onCreate 方法中的 table 结构:
@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)"
);
}
您的测试检查值是否可以按照插入时的相同顺序读出,但实际上并没有验证数据是否按预期输入。 result 和 checker 对象的所有属性都可能具有空值。一旦从数据库中读出,验证对象属性是否具有预期值。
我有这个方法:
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;
}
我的问题出在前几行。开头的游标总是 returns,计数为 0,因此使函数总是 returns null——仅在运行时! -- 我已经为这个方法编写了一个测试用例并且它通过了所以我无法弄清楚为什么它只会在运行时失败?
这里是测试用例的相关部分。我基本上将新数据插入 table 并尝试通过其 id 取回它并比较属性以确认其相同:
@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());
}
这是数据库 onCreate 方法中的 table 结构:
@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)"
);
}
您的测试检查值是否可以按照插入时的相同顺序读出,但实际上并没有验证数据是否按预期输入。 result 和 checker 对象的所有属性都可能具有空值。一旦从数据库中读出,验证对象属性是否具有预期值。