Android SQLite事务回滚工具?

时间:2011-10-04 12:30:42

标签: 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

1 个答案:

答案 0 :(得分:4)

您必须设置交易,如果成功则必须提交该交易,否则您必须取消该交易。

有关详细信息,请参阅此答案

Transaction in sqlite