Android Open Source - Udacity-Android-Course Weather Db Helper






From Project

Back to project page Udacity-Android-Course.

License

The source code is released under:

MIT License

If you think the Android project Udacity-Android-Course listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 The Android Open Source Project
 */*  w w  w  .ja  v a 2 s .  c o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.assafg.sunshine.app.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Manages a local database for weather data.
 */
public class WeatherDbHelper extends SQLiteOpenHelper {

  // If you change the database schema, you must increment the database version.
  private static final int DATABASE_VERSION = 1;

  public static final String DATABASE_NAME = "weather.db";

  public WeatherDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase sqLiteDatabase) {

    // Create a table to hold locations.  A location consists of the string supplied in the
    // location setting, the city name, and the latitude and longitude
    final String SQL_CREATE_LOCATION_TABLE = "CREATE TABLE " + WeatherContract.LocationEntry.TABLE_NAME + " (" +
        WeatherContract.LocationEntry._ID + " INTEGER PRIMARY KEY," +
        WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " TEXT UNIQUE NOT NULL, " +
        WeatherContract.LocationEntry.COLUMN_CITY_NAME + " TEXT NOT NULL, " +
        WeatherContract.LocationEntry.COLUMN_COORD_LAT + " REAL NOT NULL, " +
        WeatherContract.LocationEntry.COLUMN_COORD_LONG + " REAL NOT NULL, " +
        "UNIQUE (" + WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING +") ON CONFLICT IGNORE"+
        " );";

    final String SQL_CREATE_WEATHER_TABLE = "CREATE TABLE " + WeatherContract.WeatherEntry.TABLE_NAME + " (" +
        // Why AutoIncrement here, and not above?
        // Unique keys will be auto-generated in either case.  But for weather
        // forecasting, it's reasonable to assume the user will want information
        // for a certain date and all dates *following*, so the forecast data
        // should be sorted accordingly.
        WeatherContract.WeatherEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +

        // the ID of the location entry associated with this weather data
        WeatherContract.WeatherEntry.COLUMN_LOC_KEY + " INTEGER NOT NULL, " +
        WeatherContract.WeatherEntry.COLUMN_DATETEXT + " TEXT NOT NULL, " +
        WeatherContract.WeatherEntry.COLUMN_SHORT_DESC + " TEXT NOT NULL, " +
        WeatherContract.WeatherEntry.COLUMN_WEATHER_ID + " INTEGER NOT NULL," +

        WeatherContract.WeatherEntry.COLUMN_MIN_TEMP + " REAL NOT NULL, " +
        WeatherContract.WeatherEntry.COLUMN_MAX_TEMP + " REAL NOT NULL, " +

        WeatherContract.WeatherEntry.COLUMN_HUMIDITY + " REAL NOT NULL, " +
        WeatherContract.WeatherEntry.COLUMN_PRESSURE + " REAL NOT NULL, " +
        WeatherContract.WeatherEntry.COLUMN_WIND_SPEED + " REAL NOT NULL, " +
        WeatherContract.WeatherEntry.COLUMN_DEGREES + " REAL NOT NULL, " +

        // Set up the location column as a foreign key to location table.
        " FOREIGN KEY (" + WeatherContract.WeatherEntry.COLUMN_LOC_KEY + ") REFERENCES " +
        WeatherContract.LocationEntry.TABLE_NAME + " (" + WeatherContract.LocationEntry._ID + "), " +

        // To assure the application have just one weather entry per day
        // per location, it's created a UNIQUE constraint with REPLACE strategy
        " UNIQUE (" + WeatherContract.WeatherEntry.COLUMN_DATETEXT + ", " +
        WeatherContract.WeatherEntry.COLUMN_LOC_KEY + ") ON CONFLICT REPLACE);";

    sqLiteDatabase.execSQL(SQL_CREATE_LOCATION_TABLE);
    sqLiteDatabase.execSQL(SQL_CREATE_WEATHER_TABLE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        // Note that this only fires if you change the version number for your database.
        // It does NOT depend on the version number for your application.
        // If you want to update the schema without wiping data, commenting out the next 2 lines
        // should be your top priority before modifying this method.
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherContract.LocationEntry.TABLE_NAME);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherContract.WeatherEntry.TABLE_NAME);
        onCreate(sqLiteDatabase);
  }
}




Java Source Code List

com.example.assafg.sunshine.app.ApplicationTest.java
com.example.assafg.sunshine.app.DetailActivity.java
com.example.assafg.sunshine.app.DetailsFragment.java
com.example.assafg.sunshine.app.ForecastAdapter.java
com.example.assafg.sunshine.app.ForecastFragment.java
com.example.assafg.sunshine.app.MainActivity.java
com.example.assafg.sunshine.app.SettingsActivity.java
com.example.assafg.sunshine.app.Utility.Utility.java
com.example.assafg.sunshine.app.data.WeatherContract.java
com.example.assafg.sunshine.app.data.WeatherDbHelper.java
com.example.assafg.sunshine.app.data.WeatherProvider.java
com.example.assafg.sunshine.app.sync.SunshineAuthenticatorService.java
com.example.assafg.sunshine.app.sync.SunshineAuthenticator.java
com.example.assafg.sunshine.app.sync.SunshineSyncAdapter.java
com.example.assafg.sunshine.app.sync.SunshineSyncService.java