mercredi 2 mars 2016

sqlite constraint exception is thrown while parsing a json and storing it in db

Im trying to parse the following json

{
  "status": 1,
  "value": {
    "shipment_master": {
      "1": "Order Placed",
      "2": "In Production",
      "3": "Quality Check In progress",
      "4": "Goods received for shipment",
      "5": "Stuffing in progress",
      "6": "Cargo Shipped"
    }
  }
}

Following is the code that I use to parse it..

JSONObject value = new JSONObject(notificationResponse.getString("value"));
JSONObject shipmentMaster = value.getJSONObject("shipment_master");
Iterator<String> shipmentMasterIterator = shipmentMaster.keys();
String status = null;
String key = null;
final int numberLenth = shipmentMaster.length();
while (shipmentMasterIterator.hasNext()) {
    key = shipmentMasterIterator.next();
    status = shipmentMaster.optString(key);
   db.addShipmentMaster(new ShipmentMasterDao(Integer.valueOf(key), status));
  }

Im getting the following error,

android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)

Am I parsing the json correctly and adding it to the db properly?

This is my addShipmentMaster method

public void addShipmentMaster(ShipmentMasterDao shipmentMasterDao) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_ID, shipmentMasterDao.getId());
    values.put(KEY_STATUS, shipmentMasterDao.getStatus());

    db.insert(TABLE_SHIPMENTMASTER, null, values);
    db.close();
}`

Aucun commentaire:

Enregistrer un commentaire