Sunshine app: Create a SQLite database for Sunshine

github: S7.01-DatabaseCreationS07.02-PreventInvaildInserts , S07.03-ConflictResolutionPolicy

  1. Create a WeatherContract, in the class to create an inner class called WeatherEntry class, which implements BaseColumns, which implate, that defines the table contents of the weather table.
  2. Create a DBHelper extends SQLiteOpenHelper and implement with onCreate and onUpgrade.
  3. Prevent invalid inserts into the Weather DB. To Change each column’s type declaration to append NOT NULL, this will guarantee that every column in the database has a value, and hence won’t accept an insert statement that does not contain data for all the columns in the table!
  4. Make sure you increment the database version from 1 to 2 since we have now changed the database schema.
  5. To resolve conflicts, that is to make sure no duplicate dates exist in the database. To do so, add a UNIQUE constraint on the date column to replace on conflict, this will replace an old record with a new record if the date is the same but the remaining columns are different.
  6. Again, don’t forget to increment the database version from 2 to 3 now since we’ve changed it again.
TODO (1) Add a UNIQUE constraint on the date column to replace on conflict
                /*
                 * To ensure this table can only contain one weather entry per date, we declare
                 * the date column to be unique. We also specify "ON CONFLICT REPLACE". This tells
                 * SQLite that if we have a weather entry for a certain date and we attempt to
                 * insert another weather entry with that date, we replace the old weather entry.
                 */

                " UNIQUE (" + WeatherEntry.COLUMN_DATE + ") ON CONFLICT REPLACE);";

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s