e8d67980df51f258d90e00cc0126b8acddc3e5a7
[JavaForFun] /
1 /**
2  * Copyright 2014 Gustavo Martin Morcuende
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package name.gumartinm.weather.information.model;
17
18 import android.content.Context;
19 import android.database.sqlite.SQLiteDatabase;
20 import android.database.sqlite.SQLiteOpenHelper;
21 import android.util.Log;
22
23 public class WeatherLocationDbHelper extends SQLiteOpenHelper {
24         private static final String TAG = "LocationDbHelper";
25         public static final int DATABASE_VERSION = 1;
26     public static final String DATABASE_NAME = "Location.db";
27     
28     public WeatherLocationDbHelper(final Context context) {
29         super(context, DATABASE_NAME, null, DATABASE_VERSION);
30     }
31     
32         @Override
33         public void onCreate(final SQLiteDatabase db) {
34                 db.execSQL("CREATE TABLE " + WeatherLocationContract.WeatherLocation.TABLE_NAME + " ("
35                                 + WeatherLocationContract.WeatherLocation._ID + " INTEGER PRIMARY KEY, "
36                                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY + " TEXT" + " NOT NULL, "
37                                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY + " TEXT" + " NOT NULL, "
38                                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " INTEGER" + " NOT NULL, "
39                                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE + " INTEGER, "
40                                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE + " INTEGER, "
41                                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE + " REAL" + " NOT NULL, "
42                                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE + " REAL" + " NOT NULL, "
43                 + WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW + " INTEGER" + " NOT NULL "
44                                 + ");");
45         }
46
47         @Override
48         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49         Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
50                 + newVersion + ", which will destroy all old data");
51
52         // Kills the table and existing data
53         db.execSQL("DROP TABLE IF EXISTS " + WeatherLocationContract.WeatherLocation.TABLE_NAME);
54
55         // Recreates the database with a new version
56         onCreate(db);
57         }
58
59 }