dimanche 15 février 2015

How to insert values in sqlite?

My question is how do I store values in sqlite database that i am receiving from web service. I am new here in Android. I need some help. Thanks in advance. I searched for an appropriate answer for days now with no solution at all.


Below responseStr is the Object I am receiving as a response from webservice, whose values I would like to store in the database.



SQLiteDB sqlite_obj = new SQLiteDB(UrlActivity.this);


try
{
JSONObject mainObject = new JSONObject(responseStr);
String url = mainObject.getString("url");

if(url.equals("valid")){

//JSONObject data= new JSONObject();
//data.put("data",jsonArray2);

//List <NameValuePair> nvps2 = new ArrayList <NameValuePair>();
//nvps2.add(new BasicNameValuePair("data", mainObject.toString()));

try{
ProgressBar1.setVisibility(View.GONE);
Toast.makeText(UrlActivity.this, responseStr, Toast.LENGTH_LONG).show();

sqlite_obj.open();
sqlite_obj.deleteAll();

JSONArray jsonArray2 = new JSONArray();
jsonArray2.put(mainObject);
for (int i = 0; i < jsonArray2.length(); i++) {
JSONObject object = jsonArray2.getJSONObject(i);
//sqlite_obj.insert();


}

sqlite_obj.close();


Toast.makeText(getBaseContext(), "Stored in SQLite DB", Toast.LENGTH_LONG).show();

} catch (android.database.sqlite.SQLiteException e) {
Log.e(TAG, "SQLiteException:" + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception:" + e.getMessage());
}
}


Also, below is my SQLiteDB class.



public class SQLiteDB {
public static final String KEY_ROWID = "_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_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 = "databasetable";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
"create table databasetable (_id integer primary key autoincrement, " +

"user_id integer not null, " +
"no_of_checkin integer not null, " +
"status integer 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 databasetable");
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 user_id, Integer no_of_checkin, Integer status, String url) {

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USER_ID, user_id);
initialValues.put(KEY_NUMBER_OF_CHECKIN, no_of_checkin);
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