ForecastDbHelper.kt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.ted.weather.data.db
  2. import android.content.Context
  3. import android.database.sqlite.SQLiteDatabase
  4. import com.ted.weather.ui.App
  5. import org.jetbrains.anko.db.*
  6. class ForecastDbHelper(ctx: Context = App.instance) : ManagedSQLiteOpenHelper(ctx,
  7. ForecastDbHelper.DB_NAME, null, ForecastDbHelper.DB_VERSION) {
  8. companion object {
  9. val DB_NAME = "forecast.db"
  10. val DB_VERSION = 1
  11. val instance by lazy { ForecastDbHelper() }
  12. }
  13. override fun onCreate(db: SQLiteDatabase) {
  14. db.createTable(CityForecastTable.NAME, true,
  15. CityForecastTable.ID to INTEGER + PRIMARY_KEY,
  16. CityForecastTable.CITY to TEXT,
  17. CityForecastTable.COUNTRY to TEXT)
  18. db.createTable(DayForecastTable.NAME, true,
  19. DayForecastTable.ID to INTEGER + PRIMARY_KEY,
  20. DayForecastTable.DATE to INTEGER,
  21. DayForecastTable.DESCRIPTION to TEXT,
  22. DayForecastTable.HIGH to INTEGER,
  23. DayForecastTable.LOW to INTEGER,
  24. DayForecastTable.ICON_URL to TEXT,
  25. DayForecastTable.CITY_ID to INTEGER)
  26. }
  27. override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
  28. db.dropTable(CityForecastTable.NAME, true)
  29. db.dropTable(DayForecastTable.NAME, true)
  30. onCreate(db)
  31. }
  32. }