如何更新产品数量sqlite数据库Android?

问题描述 投票:0回答:1

CheckOrderExists 2值添加1.recipe_id 2.qty +按钮增加数量不更新。Addcart如果条件不起作用,否则条件起作用。我想在添加产品时更新产品数量。更新数量sqlite不显示

    public void Addcart(String category_name, long recipe_id, String recipe_title, String recipe_time, String recipe_image, String recipe_description, String video_url, String video_id, String content_type, String featured, String tags, long total_views, String _stringVal) {


    try {
        if (!CheckOrderExists(recipe_id).equalsIgnoreCase("0")) {
            UpdateOrderData(recipe_id, _stringVal);


            Toast.makeText(mContext, "Hello World", Toast.LENGTH_SHORT).show();

        } else {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_CAT_NAME, category_name);
            values.put(KEY_RECIPE_ID, recipe_id);
            values.put(KEY_RECIPE_TITLE, recipe_title);
            values.put(KEY_RECIPE_TIME, recipe_time);
            values.put(KEY_RECIPE_IMAGE, recipe_image);
            values.put(KEY_RECIPE_DESCRIPTION, recipe_description);
            values.put(KEY_VIDEO_URL, video_url);
            values.put(KEY_VIDEO_ID, video_id);
            values.put(KEY_CONTENT_TYPE, content_type);
            values.put(KEY_FEATURED, featured);
            values.put(KEY_TAGS, tags);
            values.put(KEY_TOTAL_VIEWS, total_views);
            values.put(KEY_QTY, _stringVal);
            // Inserting Row
            db.insert(TABLE_ADDCART, null, values);
            db.close(); // Closing database connection
        }
    } catch (Exception e) {


    }

}

CheckOrderExists ZERO IS DELECTE 表

     public String CheckOrderExists( long recipe_id) {

    String _stringVal = "0";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_ADDCART + " WHERE " + KEY_RECIPE_ID+ " = ?", new String[]{String.valueOf(recipe_id)});
    if (cursor.moveToFirst()) {
        _stringVal = cursor.getString(cursor.getColumnIndexOrThrow(KEY_QTY));


        if (_stringVal.equals("0")) {
            db.execSQL("DELETE FROM " + TABLE_ADDCART + " WHERE " + KEY_RECIPE_ID+" = ?", new String[]{String.valueOf(recipe_id)});

        }
    }
    cursor.close();
    db.close();

    return _stringVal;
}

更新订单

        public void UpdateOrderData(long recipe_id,String _stringVal) {
    SQLiteDatabase db = this.getWritableDatabase();
    if (_stringVal.equals("0")) {
        DeleteOrderData(recipe_id);
    } else {
        ContentValues values = new ContentValues();
        values.put(KEY_QTY, _stringVal);
        db.update(TABLE_ADDCART, values, recipe_id + " = ?" , new String[]{String.valueOf(recipe_id)});


    }
    db.close();
}

我希望当用户单击“添加到购物车”按钮时数量将会更新。但点击按钮两次后数量就会更新。

android sqlite
1个回答
0
投票

在每个步骤后添加更多日志记录,类似于下面的代码,在检查值之前、更改值之前打印值,这样问题肯定会很容易解决。

public void Addcart(String category_name, long recipe_id, String recipe_title, String recipe_time, String recipe_image, String recipe_description, String video_url, String video_id, String content_type, String featured, String tags, long total_views, String _stringVal) {
try {
    SQLiteDatabase db = this.getWritableDatabase();

    // Check if order already exists
    String existingQty = CheckOrderExists(recipe_id);
    
    if (!existingQty.equalsIgnoreCase("0")) {
        // If order exists, update its quantity
        UpdateOrderData(recipe_id, _stringVal);
        Toast.makeText(mContext, "Order updated", Toast.LENGTH_SHORT).show();
        Log.d("Addcart", "Order updated: Recipe ID: " + recipe_id + ", New Quantity: " + _stringVal);
    } else {
        // If order doesnt exist, insert a new row
enter code here

        ContentValues values = new ContentValues();
        values.put(KEY_CAT_NAME, category_name);
        values.put(KEY_RECIPE_ID, recipe_id);
        values.put(KEY_RECIPE_TITLE, recipe_title);
        values.put(KEY_RECIPE_TIME, recipe_time);
        values.put(KEY_RECIPE_IMAGE, recipe_image);
        values.put(KEY_RECIPE_DESCRIPTION, recipe_description);
        values.put(KEY_VIDEO_URL, video_url);
        values.put(KEY_VIDEO_ID, video_id);
        values.put(KEY_CONTENT_TYPE, content_type);
        values.put(KEY_FEATURED, featured);
        values.put(KEY_TAGS, tags);
        values.put(KEY_TOTAL_VIEWS, total_views);
        values.put(KEY_QTY, _stringVal);
        
        // Insert the new row
        db.insert(TABLE_ADDCART, null, values);
        Toast.makeText(mContext, "Order added", Toast.LENGTH_SHORT).show();
        Log.d("Addcart", "New order added: Recipe ID: " + recipe_id + ", Quantity: " + _stringVal);
    }
    db.close(); // Closing database connection
} catch (Exception e) {
    // Handle exceptions
    Log.e("Addcart", "Error adding or updating order: " + e.getMessage());
}

}

© www.soinside.com 2019 - 2024. All rights reserved.