Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite Transaction rollback facility?

Tags:

android

sqlite

I have to insert three tables independently .. but if first table is inserted successfully, then only 2nd table is needed to insert the data. If any error occurs while inserting the 2nd table, then it needs to rollback 1st table's last inserted one, likewise the 3rd table also :

I did currently like this :

   DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(RetailerOrderActivity.this);
dbAdapter.openDataBase();
   for (Map.Entry<String, MyProduct> entry : myProductMap.entrySet()) {
                    String key = entry.getKey();
                    MyProduct myProduct = entry.getValue();

                    ContentValues initialValue = new ContentValues();
                    initialValue.put("BusinessUnit",strBusinessUnit);
                    initialValue.put("ExecutiveCode",strExecutive);

                    if(salesType.equalsIgnoreCase("I")){
                        initialValue.put("InvoiceNo",transactionControl.getNextInvoiceNo());
                        initialValue.put("SalesCategory",transactionControl.getInvoicePrefix());

                    }else if(salesType.equalsIgnoreCase("O")){
                        initialValue.put("InvoiceNo",transactionControl.getNextOrderNo());
                        initialValue.put("SalesCategory",transactionControl.getOrderPrefix());
                    }
                    initialValue.put("ProductCode",key);
                    initialValue.put("LineNumber",i);
                    initialValue.put("Qty",myProduct.getQty());
                    initialValue.put("UnitPrice",myProduct.getPrice());
                    initialValue.put("DiscountValue",myProduct.getDisValue());
                    initialValue.put("DiscountQty",myProduct.getDisQty());


                    long nl = dbAdapter.insertRecordsInDB("WMInvoiceLine", null, initialValue); 

                   //update WMStockRecord table
                    if(nl != -1 ){

                        if((salesType.equalsIgnoreCase("I") && orderStockValidation.equals("1")) || (salesType.equalsIgnoreCase("O") && orderStockValidation.equals("1"))){
                            ContentValues stockValue = new ContentValues();
                            if(myProduct.getAvailableQuantity() < myProduct.getQty()){
                                stockValue.put("Stock",0.00);
                            }else{
                                double tmp = myProduct.getAvailableQuantity() - myProduct.getQty();
                                stockValue.put("Stock",tmp);
                            }
                            stockValue.put("LastUpdatedOn",strDate);
                            stockValue.put("LastUpdatedBy",strExecutive);
                            stockValue.put("ActiveStatus","1");

                            String whereCon = "BusinessUnit = '"+ strBusinessUnit +"' WarehouseCode = '"+defaultSalesWarehouse + "' LocationCode = '" + defaultSalesLocation + "' ProductCode = '" + key + "'";
                            long stock = dbAdapter.updateRecordsInDB("WMStockRecord", stockValue, whereCon, null);
                      }

                        //TO-DO WMInvoicekit
                    }

                    i++;
                   insertStatus = true;
                   lineStatus = true;
                }

but Here is not transaction not available. How we can implement the transaction facility? I am using DBAdapter

like image 486
Piraba Avatar asked Oct 04 '11 12:10

Piraba


Video Answer


1 Answers

You have to set transaction and you have to commit that transaction if it succeed else you have to cancel that transaction.

for more information you can refer this answer

Transaction in sqlite

like image 153
Dharmendra Avatar answered Oct 17 '22 07:10

Dharmendra