Cursor cursor_daily_data = db.query(TABLE_DAILY_DATA, null, null, null, null, null, null);
if (cursor_daily_data.moveToFirst()) {
do {
DailyData dailyData = new DailyData();
dailyData.time = cursor_daily_data.getString(0);
dailyData.summary = cursor_daily_data.getString(1);
dailyData.icon = cursor_daily_data.getString(2);
dailyData.sunriseTime = cursor_daily_data.getString(3);
dailyData.sunsetTime = cursor_daily_data.getString(4);
dailyData.moonPhase = cursor_daily_data.getString(5);
dailyData.precipIntensity = cursor_daily_data.getString(6);
dailyData.precipIntensityMax = cursor_daily_data.getString(7);
apiResponse.daily.data.add(dailyData);
} while (cursor_daily_data.moveToNext());
}
cursor_daily_data.close();
db.close();
有没有更好的方法来进行此操作? 我不想使用任何外部库,如糖orm ..etc
答案 0 :(得分:0)
没有外部库,没有。
无论如何,使用try
阻止它会更安全:
public List<DailyData> getAll() {
List<DailyData> list = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_DAILY_DATA, null, null, null, null, null, null);
try {
if (cursor_daily_data.moveToFirst()) {
do {
DailyData dailyData = new DailyData();
dailyData.time = cursor_daily_data.getString(0);
dailyData.summary = cursor_daily_data.getString(1);
dailyData.icon = cursor_daily_data.getString(2);
dailyData.sunriseTime = cursor_daily_data.getString(3);
dailyData.sunsetTime = cursor_daily_data.getString(4);
dailyData.moonPhase = cursor_daily_data.getString(5);
dailyData.precipIntensity = cursor_daily_data.getString(6);
dailyData.precipIntensityMax = cursor_daily_data.getString(7);
list.add(dailyData);
} while (cursor_daily_data.moveToNext());
}
} catch (Exception e) {
Log.d(TAG, "Error while trying to get data from database");
} finally {
if (cursor_daily_data != null && !cursor_daily_data.isClosed()) {
cursor_daily_data.close();
}
}
return list;
}