package com.jeeva.favourite;
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.database.sqlite.SQLiteStatement;
import android.util.Log;
public class DataHelper {
//DataBase table Create Statement
private static final String DATABASE_CREATE =
"create table ram (Story_id integer primary key, "
+ "name text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
private static final String DATABASE_NAME = "ramdb";
private static final String DATABASE_TABLE = "ram";
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DBAdapter";
public DataHelper(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
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 DataHelper 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 avgMealForTwo */
public long insert(int Story_id, String offer){
ContentValues initialValues = new ContentValues();
initialValues.put("Story_id", Story_id);
initialValues.put("offer", offer);
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 Story_id) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
"Story_id",
"offer",
},
"Story_id" + "=" + Story_id,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
OPenDb:
DataHelper db;
private void UpdateDatabaseDetails() {
try{
db=new DataHelper(this);
db.open();
Cursor sqlResult=null;
sqlResult=db.multiValueQuery("select Story_id,offer from BookUrTable");
db.close();
}
}
List
if (sqlResult.moveToFirst()) {
do {
Story_id1.add(sqlResult.getInt(0));
} while (sqlResult.moveToNext());
sqlResult.close();
if(sqlResult!=null){
size=offer1.size();
rating=new String[size];
Connect Db:
DataHelper db;
long res=-1;
db=new DataHelper(getParent());
db.open();
try{
db.insert(Story_id,offer);
res=db.singleValueQuery("select count(*) from BookUrTable where Story_id="+Story_id).simpleQueryForLong();
}catch (Exception e) {
Log.e("Dosa", "Error while Querying - "+e);
res=-1;
}
db.close();
Toast.makeText(getParent(), "Added", Toast.LENGTH_SHORT).show();
}
No comments:
Post a Comment