java - 如何在状态为 Activity 时回滚表并在状态为挂起时删除表?

标签 java android sqlite

当我在购物车列表中插入商品并使购物车处于待处理状态时,应删除购物车中存在的商品,并且购物车将清空,稍后如果我通过进入待处理列表并单击 Activity 按钮来激活该购物车,则现有商品应添加到购物车商品中,结帐时它将清除购物车。

但就我而言,当我尝试插入第二个购物车商品时,它会再次将该商品插入现有购物车中,而不是创建新的购物车。

我使用了这个查询,因为我不知道如何使用 turncate 来处理这种情况。

String query= "Select CART.CART_ID, CART.prod_id,CART.Index_id,CART.Barcode,CART.Amount,CART.Grand_Total,CART.Quantity,CART.Price,CART.status,PendingCart.P_ID,PendingCart.status,PendingCart.Created_Date,PRODUCT.Product_Price,Prod_quantity,PRODUCT.Price_Type,PRODUCT.Item_Name,PRODUCT.Image,PRODUCT.CGST,PRODUCT.SGST from PRODUCT  join CART ON CART.prod_id= PRODUCT.prod_id inner join PendingCart ON PendingCart.Index_id = CART.Index_id where CART.Index_Id= PENDINGCART.Index_id AND PendingCart.status = CART.status";


          private void AddToCart(){
            QTY= Float.parseFloat( edtQty.getText().toString());
            rate= Float.parseFloat( edtRate.getText().toString());
            Amount = Float.parseFloat( edtAmount.getText().toString() );
            unit = UnitType.getText().toString();
            Name = edtname.getText().toString();
            P_id = Integer.parseInt( rowId.getText().toString() );
            UnitType.getText().toString();
            BARCODE= txtbarcode.getText().toString();
            edtRate.clearFocus();
            edtRate.setText( String.valueOf( rate ) );
               AlertDialog.Builder builder = new AlertDialog.Builder(this);
                status= "active";
                if(!helper.isProductIdExists(P_id,status)){

                if (!helper.isStatusActive( status )){

                   helper.AddPendingCart( index_id,status);}
                    helper.AddInCart(Name,Amount,P_id,index_id,QTY,rate,BARCODE,status,Amt_with_gst_sgst );
                    txtcount.setText(Integer.toString(count));
                Toast.makeText( Home_Main.this,  "Product Added Successfully ", Toast.LENGTH_SHORT).show();
                }else{
                   builder.setMessage( "Product already added to cart.Do you want to update?");


                    builder.setPositiveButton( "YES", new DialogInterface.OnClickListener(){
                    @RequiresApi(api = Build.VERSION_CODES.N)
                    @Override
                    public void onClick(DialogInterface dialog, int id){
                        helper.UpdateCartByProduct( P_id, QTY, Amount, Amt_with_cgst_sgst);
                        Toast.makeText( Home_Main.this, "Product updated Successfully", Toast.LENGTH_SHORT ).show();
                        // User clicked OK, so save the mSelectedItems results somewhere
                        // or return them to the component that opened the dialog
                    }
                    }).setNegativeButton( "NO",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {dialog.dismiss();}} );
                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();}}

最佳答案

我认为我使用了错误的命令回滚,该命令用于手动执行事务或可以自动触发事务的情况。即您的数据已被错误删除,或者您想要备份数据。如果您有主表和第二个表(如主表),请勿使用回滚,在这种情况下这是不可能的。

我已经通过创建待处理表解决了这个问题,当用户添加购物车时,待处理 ID 将创建,当 Cart_Activity 将打开待处理项目时,将创建待处理项目,并且项目数据将处于 Activity 状态,直到我不使购物车待处理。这就是如何我解决了这个问题...如果有人遇到此类问题...可以在这里给我发消息

关于java - 如何在状态为 Activity 时回滚表并在状态为挂起时删除表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57571268/

相关文章:

java - keystore 不适用于 Java 9

android - 如何在不使用主键时进行插入或替换

java - 如何下载com.sun.net.httpserver包?

iphone - 我们可以在 SQLite 的表中创建多少列?

android - 在 Android 的 Assets 文件夹中复制两个 SQLite 数据库

javascript - 如何在 Phonegap 的 SQLITE 中检查列是否存在

java - MindSphere URL 访问

java - 设备语言对应用程序语言的影响

java - 插入到 JPA 集合而不加载它

java - Android 应用程序延迟执行不起作用