package com.login.android;
import java.util.ArrayList;
import java.util.List;




import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


 
public class DataHelper {
	   
	   static final String DATABASE_NAME = "Login.db";
	   static final int DATABASE_VERSION = 1;
	   static final String TABLE_NAME1 = "Login";
	   static final String Table_Name="Books";
	   
	   String Isbn,Name,Author,Edition,Category;
	    String Price;
	   public static final String KEY_ISBN="Isbn";
		 public static final String KEY_NAME="Name";
		 public static final String KEY_Author="Author";
		 public static final String KEY_EDITION="Edition";
		 public static final String KEY_ROWID = "id";
		 public static final String KEY_Category="Category";
		 
		 public static final String KEY_Price="Price";
		
	
		 
	   private Context context;
	   public SQLiteDatabase db;
	   OpenHelper openHelper = null;
	   
	   public DataHelper(Context context) {
	      this.context = context;
	      
	   
	   }
	   
	  
	   public void open() throws SQLException {
		   openHelper = new OpenHelper(this.context);
		
		      this.db = openHelper.getWritableDatabase();
		      this.db=openHelper.getReadableDatabase();
		     
		}
	   public  void close() {
		 
		   if (openHelper != null) {
		        openHelper.close();
		    }
		
		 }
	 
	   
	   public void insert_SignUp(String Name,String Id,String Password,String Gender) {
		   
		try{ 
			
		      this.db = openHelper.getWritableDatabase();
		      this.db=openHelper.getReadableDatabase();
		   db.execSQL("INSERT INTO "
                   + TABLE_NAME1
                   + " VALUES ('" + Name + "','" + Id +"','"+Password+ "','"+Gender+ "' );");
			
		}catch(Exception e)
	      {e.printStackTrace();
  	    
		String err = e.toString();
  	    System.out.println(err);
	      }
		
       }	   
	   
	   
	   public long add_Books(String Isbn,String Name,String Author,String Edition ,String Category,String price  ){
		   
			   this.db = openHelper.getWritableDatabase();
		      this.db=openHelper.getReadableDatabase();
		      
		      ContentValues initialValues = new ContentValues();
		      
	    	  initialValues.put(KEY_ISBN, Isbn);
	    	  initialValues.put(KEY_NAME, Name);
	    	  initialValues.put(KEY_Author, Author);
	    	  initialValues.put(KEY_EDITION, Edition);
	    	  initialValues.put(KEY_Category, Category);
	    	  initialValues.put(KEY_Price, price);
	    	  
			return db.insert(Table_Name, null, initialValues);
			}
	  
	   
	   
	   public  void delete_Books(long id){
	   
	
		   System.out.println("SSSSS");
			   this.db = openHelper.getWritableDatabase();
	      this.db=openHelper.getReadableDatabase();
		      
		      System.out.println("SSSSSqlll");
		     
		      
				try{  
  
 
					db.delete(Table_Name, KEY_ROWID+"="+id, null);
 
			}
			
				catch(Exception e)
		      {e.printStackTrace();
	  	    
	String err = e.toString();
	  	    System.out.println(err);
		      }
	      
			 }
	  
	 	   
	       public List<String> select_SignUp( String[] columns, String selection, String[] 
		                                 			                                    selectionArgs, String groupBy, 
		                                 			                                    String having, String sortBy, String sortOption) 
		          {
	    	  
	    	  
			      this.db = openHelper.getWritableDatabase();                        	   
		    	   this.db=openHelper.getReadableDatabase();
		        List<String> list3 = new ArrayList<String>();
		                                 		      Cursor cursor = this.db.query(TABLE_NAME1, columns, selection, selectionArgs, groupBy, having, sortOption);
		                                 		   
		                                 		      if (cursor.moveToFirst()) 
		                                 		      {
		                                 		    	  do 
		                                 		    	  {
		                                 		    		  list3.add(cursor.getString(0));
		                                 		    	  } while (cursor.moveToNext());
		                                 		    	 
		                                 		    	  if (cursor != null && !cursor.isClosed()) 
		                                 		    	  {
		                                 		    		  cursor.close();
		                                 		    	  }
		                                 		      }
		                                 		     
		                                 		     
		                                 		      return list3;
		                                 		     
		                                 	   }
	       
	       
	      public Cursor getAllBooks()     {    
	    
		      this.db = openHelper.getWritableDatabase();                        	   
	    	   this.db=openHelper.getReadableDatabase(); 
	    	   
	    	Cursor c=   db.query(Table_Name, new String[] {KEY_ROWID,KEY_ISBN,KEY_NAME, KEY_Author,KEY_EDITION,KEY_Price}  ,  
	    			   null,   null, null,    null, null);  
	
	    	
	    	   return  c;
	    	   } 
	      
	     
		 public Cursor getBook( String Book) throws SQLException    
	       {       
	    	   System.out.println("gettin name") ;
	    	
			      this.db = openHelper.getWritableDatabase();                        	   
		    	   this.db=openHelper.getReadableDatabase();
	    	Cursor c= db.query(Table_Name, new String[] {KEY_NAME, KEY_Author,KEY_EDITION},KEY_NAME + " = '" + Book + "'" ,null,null,null, null);
	    	
	    	
return c;
	       }
		 
		 
		 private static class OpenHelper extends SQLiteOpenHelper {
			 
	 
	      OpenHelper(Context context) {
	         super(context, DATABASE_NAME, null, DATABASE_VERSION);
	      }
	 
	      @Override
	      public void onCreate(SQLiteDatabase db) {
	         db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME1 +
	          "(Name VARCHAR2(20),Id VARCHAR2(20),Password VARCHAR2(20),Gender varchar(10))");
	         
	         db.execSQL("CREATE TABLE IF NOT EXISTS " + Table_Name +
	        " (id integer primary key , Isbn VARCHAR(14), "
	         + "Name VARCHAR2(30), "
	         + "Author VARCHAR2 (40), "
	         + "Edition VARCHAR2(6), "
	         + "Category VARCHAR2(40), "
	         + "Price VARCHAR2(20))");
	         
	      }
	 
	      @Override
	      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
	         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME1);
	         db.execSQL("DROP TABLE IF EXITS" + Table_Name);
             onCreate(db);
	      }
	     
	   }
		 

		

		
		 
}   




	
