2011-10-04 45 views
6

我必須獨立插入三個表..但如果第一個表插入成功,那麼就只需要第二個表中插入數據。如果同時插入第二個表發生任何錯誤,那麼它需要回滾第一個表的最後插入的一個,同樣是第三臺也:Android SQLite事務回滾工具?

我沒有目前這樣的:

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

,但這裏不是交易不可用。我們如何實施交易設施?我使用DBAdapter

回答

4

必須設置的交易,你必須提交該交易如果成功,否則你不得不取消該交易。

更多信息,你可以參考這個答案

Transaction in sqlite