vendredi 13 février 2015

Want to store values in sqlite database. How to do it in Android?

I am new in Android. Below is my SQLiteDB class. I want to store values in it from json web service. How would I do that? Thanks in advance. Any help would be much appreciated. Also, how can I view the database I have created?



public class SQLiteDB {

public static final String KEY_MERCHANT_KIOSK_ID = "merchant_kiosk_id";
public static final String KEY_MERCHANT_LOCATION_ID = "merchant_location_id";
public static final String KEY_USER_ID= "user_id";
public static final String KEY_NUMBER_OF_CHECKIN= "no_of_checkin";
public static final String KEY_FREE_GIFT = "free_gift";
public static final String KEY_VALID_DAYS= "valid_days";
public static final String KEY_CHECKIN_TIME_LIMIT= "checkin_time_limit";
public static final String KEY_BUSINESS_LOGO= "business_logo";
public static final String KEY_COUPON_ID="coupon_id";
public static final String KEY_COUPON_KEYWORD="coupon_keyword";
public static final String KEY_RANDOM_COUPON_CODE="random_coupon_code";
public static final String KEY_MERCHANT_ID="merchant_id";
public static final String KEY_ORGANIZATION_NAME="organization_name";
public static final String KEY_STATUS="status";
public static final String KEY_URL="url";


private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "SQLiteDB";

private static final String DATABASE_TABLE = "kiosktable";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
"create table kiosktable (merchant_kiosk_id text primary key, " +
"merchant_location_id text not null, " +
"user_id text not null, " +
"no_of_checkin text not null, " +
"free_gift text not null, " +
"valid_days text not null, " +
"checkin_time_limit text not null, " +
"business_logo text not null, " +
"coupon_id text not null, " +
"coupon_keyword text not null, " +
"random_coupon_code text not null, " +
"merchant_id text not null, " +
"organization_name text not null, " +
"status text not null, " +
"url text not null );";

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public SQLiteDB(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) {

try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}

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

//---open SQLite DB---
public SQLiteDB open() throws SQLException {

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

//---close SQLite DB---
public void close() {

DBHelper.close();
}

//---insert data into SQLite DB---
public long insert(Integer merchant_kiosk_id, Integer merchant_location_id,
Integer user_id, Integer no_of_checkin, String free_gift, Integer valid_days,
Integer checkin_time_limit, String business_logo, Integer coupon_id,
int i, Integer random_coupon_code, Integer merchant_id,
String organization_name, Integer status, String url) {

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_MERCHANT_KIOSK_ID, merchant_kiosk_id);
initialValues.put(KEY_MERCHANT_LOCATION_ID, merchant_location_id);
initialValues.put(KEY_USER_ID, user_id);
initialValues.put(KEY_NUMBER_OF_CHECKIN, no_of_checkin);
initialValues.put(KEY_FREE_GIFT, free_gift);
initialValues.put(KEY_VALID_DAYS, valid_days);
initialValues.put(KEY_CHECKIN_TIME_LIMIT, checkin_time_limit);
initialValues.put(KEY_BUSINESS_LOGO, business_logo);
initialValues.put(KEY_COUPON_ID, coupon_id);
initialValues.put(KEY_COUPON_KEYWORD, i);
initialValues.put(KEY_RANDOM_COUPON_CODE, random_coupon_code);
initialValues.put(KEY_MERCHANT_ID, merchant_id);
initialValues.put(KEY_ORGANIZATION_NAME, organization_name);
initialValues.put(KEY_STATUS, status);
initialValues.put(KEY_URL, url);


return db.insert(DATABASE_TABLE, null, initialValues);
}

//---Delete All Data from table in SQLite DB---
public void deleteAll() {

db.delete(DATABASE_TABLE, null, null);
}


}


Aucun commentaire:

Enregistrer un commentaire