Thursday, 11 June 2015

Database helper class

package com.siliconicpro.admin.ialarm;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

class DatabaseOperations extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "alarmDatabase";
    private static final int DATABASE_VERSION = 9;
    private Context context;

    public DatabaseOperations(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            // Creating SETTINGS_TABLE
            db.execSQL(SettingsTable.CREATE_SETTINGS_TABLE);
            SettingsTable.insertDefaultRow(db);

            // Creating ALARMS_TABLE
            db.execSQL(AlarmsTable.CREATE_ALARMS_TABLE);
        } catch (SQLException e) {
            Toast.makeText(context, "Database Problem", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Toast.makeText(context, "onUpgrade called", Toast.LENGTH_SHORT).show();
        db.execSQL(SettingsTable.DROP_SETTINGS_TABLE);
        db.execSQL(AlarmsTable.DROP_ALARMS_TABLE);
        onCreate(db);
    }
}

No comments:

Post a Comment