package com.example.preferences02;


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

public class DB_alarm extends SQLiteOpenHelper 
{
	//define all the constants
    public static final String DATABASE_NAME="Alarm02";
    public static final String TABLE = "table_alarm";
    public static final int DATABASE_VERSION = 1;
   
    //these are the lit of fields in the table
    public static final String STAFID="stafid";
    public static final String NAMA="namapenuh";
    public static final String JBT="jabatan";
    public static final String TELEFON="telefon";
    public static final String EMEL="emel";
    
    
    public static final String KEY_ID = "_id";
    public static final String COL_NAME = "name";
    public static final String COL_FROMDATE = "from_date";
	public static final String COL_TODATE = "to_date";
	public static final String COL_DATETIME = "datetime";
	public static final String COL_STATUS = "status";
	public static final String COL_CREATEDTIME = "created_time";
	public static final String COL_MODIFIEDTIME = "modified_time";
	public static final String COL_RULE = "rule";
	public static final String COL_INTERVAL = "interval";
	public static final String COL_SOUND = "sound";
	public static final String KEY_CREATED_AT = "created_at";
    	
	public  static final String CREATE_TABLE = "CREATE TABLE "
	            + TABLE + "(" 
	    		+ KEY_ID + " INTEGER PRIMARY KEY,"
	    		+ COL_NAME + " TEXT," 
	    		+ COL_FROMDATE + " DATE," 
	    		+ COL_TODATE + " DATE," 
	    		+ COL_DATETIME + " INTEGER," 
	    		+ COL_STATUS + " TEXT," 
	            + COL_CREATEDTIME + " INTEGER," 
	            + COL_MODIFIEDTIME + " INTEGER,"
	            + KEY_CREATED_AT + " DATETIME" + ")";
	
    public DB_alarm(Context context) 
    {
    	//create the database
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
    	//create the table
        db.execSQL(CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
    	//onUpgrade remove the existing table, and recreate and populate new data
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE);
        onCreate(db);
    }

    
    public long  insert_alarm(String NAME, String FROMDATE, String TODATE, long DATETIME, String STATUS, long CREATEDTIME, long MODIFIEDTIME)
    {
    	
    		SQLiteDatabase db = this.getWritableDatabase();
			ContentValues vals = new ContentValues();
			long now = System.currentTimeMillis();
			vals.put(COL_NAME, NAME);
			vals.put(COL_FROMDATE, FROMDATE );
			vals.put(COL_TODATE, TODATE );
			vals.put(COL_DATETIME, DATETIME);
			vals.put(COL_STATUS, STATUS);
			vals.put(COL_CREATEDTIME, CREATEDTIME );
			vals.put(COL_MODIFIEDTIME, MODIFIEDTIME );
			
			long id = db.insert(TABLE, null, vals);
			return id;
    	
    }
    
    public Cursor DB_query(String query) 
    {
    	SQLiteDatabase db = this.getWritableDatabase();
    	
    	// 1481758200000
    	//String req = "SELECT * from " +TABLE_DEBUG;
    	
    	    	
	   	 	Cursor cursor = db.rawQuery(query, null);
	   	 	if (cursor != null)
	   	 	{
	   	 		cursor.moveToFirst();
	   	 	}
	   	 	return cursor;
    }
    
}


