0
특정 검사를 위해 내 영역 테이블에서 최대 '영역 번호'를 찾기 위해 내 데이터베이스를 쿼리하려고합니다. 따라서 형식으로 텍스트를 설정할 수 있습니다. 다음 지역 번호로.데이터베이스 테이블의 최대 숫자를 쿼리하지 못했습니다
데이터베이스 테이블은 4 개의 열로 구성됩니다. _id, inpsection_link, area_number, 영역 참조.
나는 (가이드로이 게시물을 사용하여 : SQLiteDatabase.query method) : 내 데이터베이스 헬퍼 클래스에 다음 만든
public int selectMaxAreaNumber (long inspectionId) {
String inspectionIdString = String.valueOf(inspectionId);
String[] tableColumns = new String[] {
AREA_NUMBER,
"(SELECT max(" + AREA_NUMBER + ") FROM " + AREAS_TABLE + ") AS max"
};
String whereClause = INSPECTION_LINK + " = ?";
String[] whereArgs = new String[] {
inspectionIdString
};
Cursor c = rmDb.query(AREAS_TABLE, tableColumns, whereClause, whereArgs,
null, null, null);
int maxAreaNumber = c.getColumnIndex("max");
return maxAreaNumber;
}
다음과 같이 나는 다음 areaEdit 클래스에서 호출
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
Intent i = getIntent();
inspectionId = i.getLongExtra("Intent_InspectionID", -1);
areaId = i.getLongExtra("Intent_AreaID", -1);
if (areaId == -1) {
nextAreaNumber = rmDbHelper.selectMaxAreaNumber(inspectionId) + 1;
Toast.makeText(getApplicationContext(), String.valueOf(nextAreaNumber),
Toast.LENGTH_LONG).show();
}
setContentView(R.layout.edit_area);
setUpViews();
populateFields();
setTextChangedListeners();
}
그러나 매번 1을 반환합니다 (데이터베이스에 저장된 것보다 높은 숫자가 있더라도).
Confused.com !! 어떤 도움을 많이 주셨습니다.
안녕 florianmski, 감사합니다,하지만이 작동하지 않았다 - 나는 CursorIndexOutOfBoundsException을 가지고 : 인덱스 -1 요청, 나는했습니다 4 – Scamparelli
@Scamparelli의 크기 내 대답 업데이트, cursor.moveToFirst() 메서드를 잊지 마세요! – florianmski
정말 감사합니다. 테이블에 데이터가없는 경우와 별도로 작동하는 것 같습니다. 내가 null을 테스트해야 할 것 같아요,하지만 나를 보일 것 같지 않습니다 - int에 대한 대안이 있습니까? 건배. – Scamparelli