package com.example.contactslist;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class SQLiteContactHelper extends SQLiteOpenHelper {
	private static final String DB_NAME = "contactsdb.db";
	private static final int DB_VERSION = 1;

	public static final String CONTACT_TABLE = "contacts";
	public static final String COL_CONTACT_ID = BaseColumns._ID;
	public static final String COL_CONTACT = "name";
	public static final String COL_CONTACT_NUM = "number";

	// Database creation sql statement
	private static final String DATABASE_CREATE = "create table " + CONTACT_TABLE
			+ " (" + COL_CONTACT_ID + " integer primary key autoincrement, "
			+ COL_CONTACT + " text, " + COL_CONTACT_NUM + " text);";

	public SQLiteContactHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL("DROP TABLE IF EXISTS " + CONTACT_TABLE);
		database.execSQL(DATABASE_CREATE);

		database.execSQL("insert into "
				+ CONTACT_TABLE
				+ " ('"+COL_CONTACT+"', '"+COL_CONTACT_NUM+"') values('SK','0999999999')");
		//Insert initial data here
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(SQLiteContactHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		// db.execSQL("DROP DATABASE IF EXISTS " + DB_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + CONTACT_TABLE);
		onCreate(db);
	}

}
