Android Open Source - Weather-app Weather Db Helper






From Project

Back to project page Weather-app.

License

The source code is released under:

Apache License

If you think the Android project Weather-app 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

package app.sunshine.juanjo.util;
/* w  ww  .ja va 2s .  c  om*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import app.sunshine.juanjo.util.WeatherContract.LocationEntry;
import app.sunshine.juanjo.util.WeatherContract.WeatherEntry;

/**
 * Created by juanjo on 17/08/14.
 */
public class WeatherDbHelper extends SQLiteOpenHelper {
  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 db) {
    // 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

    // TBD

    final String SQL_CREATE_WEATHER_TABLE = "CREATE TABLE "
        + 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.
        WeatherEntry._ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        +

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

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

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

        // Set up the location column as a foreign key to location
        // table.
        " FOREIGN KEY (" + WeatherEntry.COLUMN_LOC_KEY + ") REFERENCES "
        + LocationEntry.TABLE_NAME + " ("
        + 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 (" + WeatherEntry.COLUMN_DATETEXT + ", " + WeatherEntry.COLUMN_LOC_KEY
        + ") ON CONFLICT REPLACE);";

    final String SQL_CREATE_LOCATION_TABLE = "CREATE TABLE " + LocationEntry.TABLE_NAME + " ("
        + LocationEntry._ID + " INTEGER PRIMARY KEY,"
        + LocationEntry.COLUMN_LOCATION_SETTING + " TEXT UNIQUE NOT NULL, "
        + LocationEntry.COLUMN_CITY + " TEXT NOT NULL, " + LocationEntry.COLUMN_LATITUDE
        + " REAL NOT NULL, " + LocationEntry.COLUMN_LONGITUDE + " REAL NOT NULL, "
        + "UNIQUE (" + LocationEntry.COLUMN_LOCATION_SETTING + ") ON CONFLICT IGNORE"
        + " );";

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

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + LocationEntry.TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME);
    onCreate(db);
  }
}




Java Source Code List

app.sunshine.juanjo.gcm.GcmBroadcastReceiver.java
app.sunshine.juanjo.gcm.GcmIntentService.java
app.sunshine.juanjo.network.APIHTTP.java
app.sunshine.juanjo.network.HttpRequest.java
app.sunshine.juanjo.network.SendIdGCM.java
app.sunshine.juanjo.sync.SunshineAuthenticatorService.java
app.sunshine.juanjo.sync.SunshineAuthenticator.java
app.sunshine.juanjo.sync.SunshineSyncAdapter.java
app.sunshine.juanjo.sync.SunshineSyncService.java
app.sunshine.juanjo.util.Utility.java
app.sunshine.juanjo.util.WeatherContract.java
app.sunshine.juanjo.util.WeatherDbHelper.java
app.sunshine.juanjo.util.WeatherJsonParser.java
app.sunshine.juanjo.util.WeatherProvider.java
app.sunshine.juanjo.views.MyView.java
app.sunshine.juanjo.views.activities.DetailActivity.java
app.sunshine.juanjo.views.activities.MainActivity.java
app.sunshine.juanjo.views.activities.SettingsActivity.java
app.sunshine.juanjo.views.adapter.ForecastAdapter.java
app.sunshine.juanjo.views.fragments.DetailFragment.java
app.sunshine.juanjo.views.fragments.ForeCastFragment.java