Monday 14 November 2011

DATABASE

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;
import android.widget.Toast;

public class DataBase33 {


public static final String Row_categoryID="_categoryid";
public static final String Row_CategoryName="CategoryName";
public static final String Table_Name3="CategoryTable";

private static final String DataBase_Name="foodDataBase33";
private static final int version =1;
private static final String TAG = "DBAdapter";

private static final String DATA_CREATE2="create table CategoryTable(_categoryid integer primary key autoincrement,"+ " CategoryName text not null);";

private static Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DataBase33(Context con)
{
this.context=con;
DBHelper=new DatabaseHelper(con);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
       DatabaseHelper(Context context)
       {
           super(context, DataBase_Name, null, version);
       }

   
       public void onCreate(SQLiteDatabase db)
       {
       
           db.execSQL(DATA_CREATE2);
       }
 
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
       {
           Log.w(TAG, "Upgrading dataBase from version " + oldVersion + " to "+ newVersion + ", which will destroy all old data");
           db.execSQL("DROP TABLE IF EXISTS note_table");
           onCreate(db);
       }
   }  

public DataBase33 open() throws  Exception
{
db = DBHelper.getWritableDatabase();
       return this;
}

public void close()
{
DBHelper.close();
}

public long insertcategory(String categoryname)
{
ContentValues cv1=new ContentValues();
cv1.put(Row_CategoryName, categoryname);
Log.d("karannnnnnnnnn11111111111111111", categoryname);
return db.insert(Table_Name3, null, cv1);
}

public Cursor getcategory() throws SQLException
{
return db.query( Table_Name3,  new String[]{ Row_categoryID , Row_CategoryName }, null,null,null,null,null);
}
 
public Cursor getAllcategory()
{
Cursor rt= db.rawQuery("select * from  CategoryTable",null);
return rt;
}


}

No comments:

Post a Comment