package findyourfate.com.porutham;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DBHelper extends SQLiteOpenHelper{
	private SQLiteDatabase db;
	public static Context context;
	DBHelper DB = null;
	public static final String KEY_ROWID = "_id";
	public static final String KEY_BOYSTAR = "boystar";
	public static final String KEY_GIRLSTAR = "girlstar";
	public static final String KEY_DHINAM = "dhinam";
	public static final String KEY_GANAM = "ganam";
	public static final String KEY_MAKENDIRAM = "makendiram";
	public static final String KEY_DEERKAM = "deerkam";
	public static final String KEY_YONI = "yoni";
	public static final String KEY_RASI = "rasi";
	public static final String KEY_ATHIPATHI = "athipathi";
	public static final String KEY_VASIYAM = "vasiyam";
	public static final String KEY_RAJJU = "rajju";
	public static final String KEY_NAADI = "naadi";
	public static final String KEY_SCORE = "score";
	public static final String KEY_RMK = "rmk";
	public static final String KEY_SPECIAL = "special";
	private static final String DATABASE_NAME = "poo2";
	private static final int DATABASE_VERSION = 3;
	public static final String DATABASE_TABLE_NAME = "poo1";
	
	private static final String DATABASE_TABLE_CREATE =
	            "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE_NAME + "(" +
	            "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
	            "boystar TEXT NOT NULL, girlstar TEXT NOT NULL, dhinam TEXT NOT NULL,  ganam TEXT NOT NULL,makendiram TEXT NOT NULL,deerkam TEXT NOT NULL, yoni TEXT NOT NULL, rasi TEXT NOT NULL, athipathi TEXT NOT NULL, vasiyam TEXT NOT NULL, rajju TEXT NOT NULL, naadi TEXT NOT NULL, score TEXT NOT NULL, rmk TEXT NOT NULL, special TEXT NOT NULL);";
	    public DBHelper(Context context) {
	    	  super(context, DATABASE_NAME, null, DATABASE_VERSION);
	    	     try {
	    	    	 File dbFile = context.getDatabasePath(DATABASE_NAME);	    	    	
	    	    	InputStream is = context.getAssets().open(DATABASE_NAME);	    	      
	    	     OutputStream os = new FileOutputStream(dbFile);
	    	     byte[] buffer = new byte[1024];
	    	     int mLength;
	    	     while ((mLength = is.read(buffer))>0)
	    	     {
	    	         os.write(buffer, 0, mLength);
	    	     }
	    	     os.flush();
	    	     os.close();
	    	     is.close();
	    	} catch (IOException e) {
	    		e.printStackTrace();
	    	}
	    	}
	    public SQLiteDatabase openDatabase() {
	        File dbFile = context.getDatabasePath(DATABASE_NAME);	         
	        Toast.makeText(context, "3", Toast.LENGTH_SHORT).show();
	        return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
	    }
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 		
	    		db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE_NAME);
	    	    onCreate(db);	    	 
	    	}
	    	public Cursor rawQuery(String string, String[] strings) {
	    	 return null;
	    	}
	    	public void open() {	    	 
	    	 getWritableDatabase(); 
	    	}    	
	    	public void onCreate(SQLiteDatabase db) {	    		  
	    		  db.execSQL(DATABASE_TABLE_CREATE);	    		   
	    		}
	    		public Cursor getDetails(String text) throws SQLException 
	    		{	    		 
	    		    Cursor mCursor =
	    		            db.query(true, DATABASE_TABLE_NAME, 
	    		              new String[]{KEY_ROWID, KEY_BOYSTAR, KEY_GIRLSTAR, KEY_DHINAM , KEY_GANAM, KEY_MAKENDIRAM , KEY_DEERKAM, KEY_YONI, KEY_RASI, KEY_ATHIPATHI,KEY_VASIYAM ,KEY_RAJJU , KEY_NAADI , KEY_SCORE, KEY_RMK , KEY_SPECIAL}, 
	    		              KEY_BOYSTAR + "=" + text, 
	    		              null, null, null, null, null);
	    		    if (mCursor != null) 
	    		    {
	    		        mCursor.moveToFirst();
	    		    }
	    		    return mCursor;
	    		}  	
}
