19 Saving and requesting data from database
83
1
class ForecastDb(
2
val forecastDbHelper: ForecastDbHelper = ForecastDbHelper.instance,
3
val dataMapper: DbDataMapper = DbDataMapper()) {
4
...
5
}
Both functions will make use of the
use()
function we saw in the previous chapter. The value that
the lambda returns will be used as the result of our function. So let’s define a function that requests
a forecast based on a zip code and a date:
1
fun requestForecastByZipCode(zipCode: Long, date: Long) = forecastDbHelper.use {
2
....
3
}
Not much to explain here: we return the result of the
use
function as the result of our function.
Requesting a forecast
The first request that needs to be done is the daily forecast, because we need the list to create the
city object. Anko provides a simple request builder, so let’s take advantage of it:
1
val dailyRequest = "${DayForecastTable.CITY_ID} = ? " +
2
"AND ${DayForecastTable.DATE} >= ?"
3
4
val dailyForecast = select(DayForecastTable.NAME)
5
.whereSimple(dailyRequest, zipCode.toString(), date.toString())
6
.parseList { DayForecast(HashMap(it)) }
The first line,
dailyRequest
, is the
where
Do'stlaringiz bilan baham: