我有以下代码从SQLite数据库中读取数据。我可以检测到空游标,但我不知道如何逃避它。我无法将return语句移到if( result != null && result.moveToFirst()
语句中。
我得到最后两个日志行,然后程序崩溃
/CSV Import﹕ cursor index is NOT greater than 0
/CSV Import﹕ Null Cursor Found
代码:
public Equipment getEquipmentByID(int id){
SQLiteDatabase db = this.getReadableDatabase();
String SQL = "SELECT "+EQUIPMENT_TABLE+".*, "+LOCATION_TABLE+"."+LOCATION_NAME+" as locationName FROM "+ EQUIPMENT_TABLE + " " +
"JOIN "+LOCATION_TABLE + " ON "+LOCATION_TABLE+"."+LOCATION_ID+"="+EQUIPMENT_TABLE+"."+EQUIPMENT_LOCATION+" " +
"WHERE "+EQUIPMENT_TABLE+"."+EQUIPMENT_ID +"="+id;
Cursor result = db.rawQuery(SQL, null);
result.moveToFirst();
if(result != null && result.moveToFirst()) {
Log.v("CSV Import", "Null Cursor not Found");
} else {
if (result.getCount()>0) {
Log.v("CSV Import", "cursor index is grater than 0");
} else {
Log.v("CSV Import", "cursor index is NOT greater than 0");
}
Log.v("CSV Import", "Null Cursor Found");
}
Equipment equipment = new Equipment(result.getInt(result.getColumnIndex(EQUIPMENT_ID)), result.getString(result.getColumnIndex(EQUIPMENT_NAME)),
result.getString(result.getColumnIndex(EQUIPMENT_MODEL)), result.getString(result.getColumnIndex(EQUIPMENT_SERIAL)),
result.getString(result.getColumnIndex(EQUIPMENT_NOTES)), new Location(result.getInt(result.getColumnIndex(EQUIPMENT_LOCATION)),
result.getString(result.getColumnIndex("locationName"))), new EquipmentType(1, "Undefined"));
return equipment;
}
答案 0 :(得分:1)
From the docs,如果光标为空,moveToFirst()
将返回false,否则为true。所以从第一行开始:result.moveToFirst();
将所有内容替换为:
if(result == null) return null;
if(!result.moveToFirst()) {
result.close();
db.close();
return null;
}
Equipment equipment = new Equipment(result.get //.... and so on ....//);
result.close();
db.close();
return equipment;
答案 1 :(得分:0)
结果永远不为空,你的代码错了,这对你很好
public Equipment getEquipmentByID(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
String SQL = "SELECT "+EQUIPMENT_TABLE+".*, "+LOCATION_TABLE+"."+LOCATION_NAME+" as locationName FROM "+ EQUIPMENT_TABLE + " " +
"JOIN "+LOCATION_TABLE + " ON "+LOCATION_TABLE+"."+LOCATION_ID+"="+EQUIPMENT_TABLE+"."+EQUIPMENT_LOCATION+" " +
"WHERE "+EQUIPMENT_TABLE+"."+EQUIPMENT_ID +"="+id;
Cursor result = db.rawQuery(SQL, null);
//result.moveToFirst();
if(result.moveToFirst()) {
//Log.v("CSV Import", "Null Cursor not Found");
Equipment equipment = new Equipment(result.getInt(result.getColumnIndex(EQUIPMENT_ID)), result.getString(result.getColumnIndex(EQUIPMENT_NAME)),
result.getString(result.getColumnIndex(EQUIPMENT_MODEL)), result.getString(result.getColumnIndex(EQUIPMENT_SERIAL)),
result.getString(result.getColumnIndex(EQUIPMENT_NOTES)), new Location(result.getInt(result.getColumnIndex(EQUIPMENT_LOCATION)),
result.getString(result.getColumnIndex("locationName"))), new EquipmentType(1, "Undefined"));
return equipment;
}
else
{
Log.v("CSV Import", "cursor index is NOT greater than 0");
//if (result.getCount()>0)
// {Log.v("CSV Import", "cursor index is grater than 0");}
//else
//{Log.v("CSV Import", "cursor index is NOT greater than 0");}
//Log.v("CSV Import", "Null Cursor Found");
}
result.close;
}