解析json并将其存储在db中时抛出sqlite约束异常

时间:2016-03-02 09:40:48

标签: android json sqlite

我试图解析以下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"
    }
  }
}

以下是我用来解析它的代码..

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));
  }

我收到以下错误,

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

我是否正确解析了json并正确地将其添加到数据库?

这是我的addShipmentMaster方法

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();
}`

0 个答案:

没有答案