• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Adding Json object to SQLite database

msyhles

Lurker
Good day fellow Androidees, i am trying to add a json String i got using the AsyncTask function this JSON string contains database results from a mySql database. So upon adding the JSON objet into a sqlite database(which works fine). i am struggling to update the database if with the same desire json string if the table in the sqlite database already has an id contained in the json object.

My strategy is to first DELETE FROM TABLE WHERE id is = ID_IN_JSON_OBJECT then add the json objects to database after that, however my log cat tells me that it adds one row then proceeds to delete adds another row then proceeds to delete again, i want to delete all then add in that sequence. please help heres my code below.

Code:
public void addSMS(int sendid, String number, String sendtype){
    //Cursor cursor = null;

    String selectQuery = "SELECT * FROM " + SMS_Table + " WHERE "+col_id+ " = " +sendid;
    SQLiteDatabase db = this.getWritableDatabase();
    cursor = db.rawQuery(selectQuery, null);
    ContentValues values = new ContentValues();
    values.put(col_id, sendid);
    values.put(col_num, number);
    values.put(column_type, sendtype);
 if(cursor.getCount()>0 ) {

       //db.delete(SMS_Table, "sendid=?", new String[] {Integer.toString(sendid)});
     //Log.d(TAG, "deleted " + number);
    db.update(SMS_Table, values, "sendid=?", new String[] {Integer.toString(sendid)});
    Log.d(TAG, "updated " + number);
       
    }else{
        db.insert(SMS_Table, null, values);
Log.d(TAG, "New Record " + number);

}

    cursor.close();
    db.close();
 
Last edited by a moderator:
Back
Top Bottom