Friday, September 16, 2011

Database

Step 1:


public class DataCoupon{

private static final String DATABASE_CREATE =
"create table ram (1 integer primary key, "
+ "2 text,3 text not null,4 text);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "ram";
private static final int DATABASE_VERSION = 1;
private static final String TAG = "database";

public DataCoupon(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

public void deleteAll() {
db.delete(DATABASE_TABLE, null, null);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}

@Override
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 "+DATABASE_TABLE);
onCreate(db);
}

}

/**---opens the database---*/
public DataCoupon open() throws SQLException
{

db = DBHelper.getWritableDatabase();
return this;
}

/**---closes the database---*/
public void close()
{
DBHelper.close();
}

/**---deletes a particular title---*/
public boolean deleteFeed(long Story_id)
{
return db.delete(DATABASE_TABLE, "Story_id" +
"=" + Story_id, null) > 0;
}

/**---insert a title into the database---
* @param date
* @param avgMealForTwo */

public long insert(int 1, String 2, String 3, String 4){

ContentValues initialValues = new ContentValues();
initialValues.put("1", 1);
initialValues.put("2", 2);
initialValues.put("3", 3);
initialValues.put("4", 4);
return db.insert(DATABASE_TABLE, null, initialValues);

}

/**---Get multiValue Result of user Query--g*/
public Cursor multiValueQuery(String userSQL){
Cursor cursorResult=null;
try{
cursorResult=db.rawQuery(userSQL, null);


}catch(Exception e){
System.out.println("Error in select all--"+e);
return null;
}
if (cursorResult != null) {
cursorResult.moveToFirst();
}
return cursorResult;
}



/**-- Give a single value as result for our give query in compileStatement--g*/
public SQLiteStatement singleValueQuery(String userSQL){
SQLiteStatement result=null;
try{

result=db.compileStatement(userSQL);

return result;

}catch(Exception e){
System.out.println("User Query Error"+e);
return null;
}
}

//---retrieves a particular title---
public Cursor getFeed(Integer 1) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
"1",
"2",
"3",
"4"
},
"1" + "=" + 1,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}


}


Step 2:

DataCoupon saved;

saved=new DataCoupon(this);
saved.open();
Cursor res=saved.getFeed(id);
if(res==null || res.getCount()<=0){
saved.open();
try{
saved_coupon.insert(id,2,3,4);
}catch (Exception e) {
Log.e("tag", "Error while Querying - "+e);
}
saved.close();

Toast.makeText(this, "downloaded", Toast.LENGTH_SHORT).show();

res.close();
}
else{
Toast.makeText(getApplicationContext(), "This already downloaded", Toast.LENGTH_SHORT).show();
res.close();
}
saved.close();

No comments:

Post a Comment