I have the following code that is reading data from a SQLite db. I can detect the null cursor, but I do not know how to escape from it. I can not move the return statement to within the if( result != null && result.moveToFirst() statement... I get the last two log lines then the program crashes
/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;
}
'
Aucun commentaire:
Enregistrer un commentaire