· 9 years ago · Dec 28, 2016, 07:01 PM
1package com.example.android.sunshine.app.data;
2
3import android.content.Context;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6
7import com.example.android.sunshine.app.data.WeatherContract.LocationEntry;
8import com.example.android.sunshine.app.data.WeatherContract.WeatherEntry;
9
10/** Manages a local database for weather data. */
11public class WeatherDbHelper extends SQLiteOpenHelper {
12
13 // If you change the database schema, you must increment the database version.
14 private static final int DATABASE_VERSION = 2;
15
16 static final String DATABASE_NAME = "weather.db";
17
18 public WeatherDbHelper(Context context) {
19 super(context, DATABASE_NAME, null, DATABASE_VERSION);
20 }
21
22 @Override
23 public void onCreate(SQLiteDatabase sqLiteDatabase) {
24 final String SQL_CREATE_WEATHER_TABLE =
25 "CREATE TABLE " + WeatherEntry.TABLE_NAME + " (" +
26 // Why AutoIncrement here, and not above?
27 // Unique keys will be auto-generated in either case. But for weather
28 // forecasting, it's reasonable to assume the user will want information
29 // for a certain date and all dates *following*, so the forecast data
30 // should be sorted accordingly.
31 WeatherEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
32
33 // the ID of the location entry associated with this weather data
34 WeatherEntry.COLUMN_LOC_KEY + " INTEGER NOT NULL, " +
35 WeatherEntry.COLUMN_DATE + " INTEGER NOT NULL, " +
36 WeatherEntry.COLUMN_SHORT_DESC + " TEXT NOT NULL, " +
37 WeatherEntry.COLUMN_WEATHER_ID + " INTEGER NOT NULL," +
38
39 WeatherEntry.COLUMN_MIN_TEMP + " REAL NOT NULL, " +
40 WeatherEntry.COLUMN_MAX_TEMP + " REAL NOT NULL, " +
41
42 WeatherEntry.COLUMN_HUMIDITY + " REAL NOT NULL, " +
43 WeatherEntry.COLUMN_PRESSURE + " REAL NOT NULL, " +
44 WeatherEntry.COLUMN_WIND_SPEED + " REAL NOT NULL, " +
45 WeatherEntry.COLUMN_DEGREES + " REAL NOT NULL, " +
46
47 // Set up the location column as a foreign key to location table.
48 " FOREIGN KEY (" + WeatherEntry.COLUMN_LOC_KEY + ") REFERENCES " +
49 LocationEntry.TABLE_NAME + " (" + LocationEntry._ID + "), " +
50
51 // To assure the application have just one weather entry per day
52 // per location, it's created a UNIQUE constraint with REPLACE strategy
53 " UNIQUE (" + WeatherEntry.COLUMN_DATE + ", " +
54 WeatherEntry.COLUMN_LOC_KEY + ") ON CONFLICT REPLACE);";
55
56 sqLiteDatabase.execSQL(SQL_CREATE_WEATHER_TABLE);
57
58 final String SQL_CREATE_LOCATION_TABLE =
59 "CREATE TABLE " + LocationEntry.TABLE_NAME + " (" +
60 LocationEntry._ID + " INTEGER PRIMARY KEY, " +
61 LocationEntry.COLUMN_LOCATION_SETTING + " TEXT UNIQUE NOT NULL, " +
62 LocationEntry.COLUMN_CITY_NAME + " TEXT NOT NULL, " +
63 LocationEntry.COLUMN_COORD_LAT + " REAL NOT NULL, " +
64 LocationEntry.COLUMN_COORD_LONG + " REAL NOT NULL);";
65
66 sqLiteDatabase.execSQL(SQL_CREATE_LOCATION_TABLE);
67
68
69 }
70
71 @Override
72 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
73 // This database is only a cache for online data, so its upgrade policy is
74 // to simply to discard the data and start over
75 // Note that this only fires if you change the version number for your database.
76 // It does NOT depend on the version number for your application.
77 // If you want to update the schema without wiping data, commenting out the next 2 lines
78 // should be your top priority before modifying this method.
79 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + LocationEntry.TABLE_NAME);
80 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME);
81 onCreate(sqLiteDatabase);
82 }
83}