0
나는이 내 데이터베이스 클래스에서 다음과 같은 방법단순화 원시 SQL 문
public ArrayList<HouseType> getAllHouseTypes(AdvertTypeEnum advertTypeEnum) {
String language = Locale.getDefault().getLanguage();
ArrayList<HouseType> houseTypes = new ArrayList<HouseType>();
// Get all house types for this advert type
String selectQuery = "select ht._id, htt." + language + ", htat.api_id " +
"from house_types ht, house_types_advert_types htat, house_types_translation htt " +
"where ht._id=htat.house_type_id and ht._id=htt.house_type_id and htat.advert_type=?";
Cursor cursor = database.rawQuery(selectQuery, new String[]{String.valueOf(advertTypeEnum.getValue())});
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
HouseType houseType = new HouseType();
houseType.setId(cursor.getInt(cursor.getColumnIndex(HouseTypesDBContract.HouseTypes.COLUMN_ID)));
houseType.setApiId(cursor.getInt(cursor.getColumnIndex(HouseTypesDBContract.HouseTypesAdvertTypes.COLUMN_API_ID)));
houseType.setName(cursor.getString(cursor.getColumnIndex(language)));
houseType.setAdvertTypeEnum(advertTypeEnum);
houseTypes.add(houseType);
} while (cursor.moveToNext());
}
cursor.close();
return houseTypes;
}
안드로이드에서이 쿼리를 수행하는 청소 방법은 무엇입니까? 열 이름을 상수로 바꾸면 훨씬 더 복잡해 지므로이 작업을 수행하는 것이 더 좋은지 궁금합니다.
단지의 offtopic 코멘트 : "... ...." + language + "..."'나쁜 테이블 디자인처럼 보입니다. house_types_translation은 house_types_translation (house_type_id, lang1, lang2, lang3, langN)이 아니라 house_type_id (PK (house_type_id, language), value)'... 이제 당신은 statment ...에서 언어를 선택할 수 있습니다. 또한 tab1, tab2에서 "old joins"를 사용하고 있습니다. 여기서 tab1.pk = tab2.fk' tab1에서의 join1과 같이 사용합니다. tab1에서 내부 join tab2와 같습니다. pk = tab2.fk' – Selvin