lundi 14 mars 2016

SQLiteConstraintException is being thrown each time while adding dynamic datas to a table

I have created a table and adding elements to table as follows,

`

public void addPendingOrders(PendingOrdersDao pendingOrders) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_ID, pendingOrders.getId());
        values.put(KEY_SHIPMENT_NO, pendingOrders.getSipmentNo());
        values.put(KEY_SHIPMENT_REF_NO, pendingOrders.getShipmentRefNo());
        values.put(KEY_PRO_INV_NO, pendingOrders.getProformoInvoiceNumber());
        values.put(KEY_PRO_INV_DATE, pendingOrders.getProformoInvoiceDate());
        values.put(KEY_SHIPMENT_DATE, pendingOrders.getShipmentDate());
        values.put(KEY_PT_OF_CONTACT, pendingOrders.getPointOfContact());
        values.put(KEY_PRODUCT, pendingOrders.getProduct());
        values.put(KEY_QTY, pendingOrders.getQuantity());
        values.put(KEY_RATE, pendingOrders.get_rate());
        values.put(KEY_AMOUNT, pendingOrders.getAmount());
        values.put(KEY_PRODUCT_IMG, pendingOrders.getProductImage());
        values.put(KEY_SHIPMENT_STAT, pendingOrders.getShipmentStatus());

        // Inserting Row
        db.insert(TABLE_PENDINGORDERS, null, values);
        db.close(); // Closing database connection
    }

Then in my code, Im using the following code to add values to the table

for (PendingOrdersDao cn : allPendingOrders) {
                                        //int _id, String _shipmentNo, String _shipmentRefNo, String _proformoInvoiceNumber, String _proformoInvoiceDate, String _shipmentDate, String _pointOfContact, String _product, String _quantity, String _rate, String _amount, String _productImage, String _shipmentStatus) {



myitems.add(new PendingOrdersDao(cn.getId(), cn.getSipmentNo(), cn.getShipmentRefNo(), cn.getProformoInvoiceNumber(), cn.getProformoInvoiceDate(), cn.getShipmentDate(), cn.getPointOfContact(), cn.getProduct(), cn.getQuantity(), cn.get_rate(), cn.getAmount(), cn.getProductImage(), cn.getShipmentStatus()));

                                }

Each time when a view is clicked, the values will be retrieved using volley and those values will be stored in database. First time when the view is clicked, the datas are added to the database without any issues. Next time if Im clicking the view, the new values are getting retrieved once again but the SQLiteConstraintException error is being thrown when newly fetched datas are being added to the table. I guess its because the id's of old values in the table and new values that are being inserted to the table are all same. Is there any ways through which I can handle this error? How can I clear the records in a table and insert newly fetched datas without getting sqlite constraint exception?

Aucun commentaire:

Enregistrer un commentaire