如何在SQLite Android中区分相同的ID值

时间:2018-09-07 20:25:31

标签: android sqlite android-sqlite sqliteopenhelper

我要解决的问题是我认为的架构。我需要知道如何区分颜色相同的值。例如,我有一个颜色和尺寸不同的商品。如果用户选择了相同的数据(颜色和大小),并且如果用户选择了具有不同数据的相同项目(颜色和大小),哪种模式可以帮助我覆盖?我提供了数据库类和两个屏幕截图。

public class Database extends SQLiteAssetHelper {

// Database Name
private static final String DB_NAME = "AVHDB.db";

// Database Version
private static final int DB_VERSION = 1;

// Parameterized Constructor
public Database(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

// Function for getting all items in a Cart
public List<Order> getCarts(String uId) {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = { "UserId", "ItemId", "ItemName", "ItemImage", "ItemPrice", "ItemStock", "ItemMaterial", "ItemBrand", "ItemCondition", "ItemStyle", "ItemColorValue", "ItemColorName", "ItemSize", "ItemQuantity" };
    String sqlTable = "OrderDetail";

    qb.setTables(sqlTable);
    Cursor cursor = qb.query(db, sqlSelect, "UserId=?", new String[] {uId}, null, null, null);

    final List<Order> result = new ArrayList<>();
    if (cursor.moveToFirst()) {
        do {
            result.add(new Order(
                    cursor.getString(cursor.getColumnIndex("UserId")),
                    cursor.getString(cursor.getColumnIndex("ItemId")),
                    cursor.getString(cursor.getColumnIndex("ItemName")),
                    cursor.getString(cursor.getColumnIndex("ItemImage")),
                    cursor.getString(cursor.getColumnIndex("ItemPrice")),
                    cursor.getString(cursor.getColumnIndex("ItemStock")),
                    cursor.getString(cursor.getColumnIndex("ItemMaterial")),
                    cursor.getString(cursor.getColumnIndex("ItemBrand")),
                    cursor.getString(cursor.getColumnIndex("ItemCondition")),
                    cursor.getString(cursor.getColumnIndex("ItemStyle")),
                    cursor.getString(cursor.getColumnIndex("ItemColorValue")),
                    cursor.getString(cursor.getColumnIndex("ItemColorName")),
                    cursor.getString(cursor.getColumnIndex("ItemSize")),
                    cursor.getString(cursor.getColumnIndex("ItemQuantity"))
                    ));
        } while (cursor.moveToNext());

    }
    return result;
}

// Function for checking if items exists in Cart
public boolean checkItemExists(String itemId, String uId) {
    boolean flag;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor;
    String SQLQuery = String.format("SELECT * FROM OrderDetail WHERE UserId='%s' AND ItemId='%s'", uId, itemId);
    cursor = db.rawQuery(SQLQuery, null);
    flag = cursor.getCount() > 0;
    cursor.close();
    return flag;
}

// Function for adding items to Cart
public void addToCart(Order order) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format(
            "INSERT OR REPLACE INTO OrderDetail(UserId, ItemId, ItemName, ItemImage, ItemPrice, ItemStock, ItemMaterial, ItemBrand, ItemCondition, ItemStyle, ItemColorValue, ItemColorName, ItemSize, ItemQuantity) " +
            "VALUES('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
            order.getUserId(),
            order.getItemId(),
            order.getItemName(),
            order.getItemImage(),
            order.getItemPrice(),
            order.getItemStock(),
            order.getItemMaterial(),
            order.getItemBrand(),
            order.getItemCondition(),
            order.getItemStyle(),
            order.getItemColorValue(),
            order.getItemColorName(),
            order.getItemSize(),
            order.getItemQuantity());
    db.execSQL(query);
}

// Function for cleaning Cart
public void cleanCart() {
    SQLiteDatabase db = getReadableDatabase();
    String query = "DELETE FROM OrderDetail";
    db.execSQL(query);
}

// Function for getting the count of items in a Cart
public int getCountCart(String uId) {
    int count = 0;
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("SELECT COUNT(*) FROM OrderDetail WHERE UserId='%s'", uId);
    Cursor cursor = db.rawQuery(query, null);
    try {
        if (cursor.moveToFirst()) {
            do {
                count = cursor.getInt(0);
            } while (cursor.moveToNext());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return count;
}

// Function for updating Cart
public void updateCart(Order order) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("UPDATE OrderDetail SET ItemQuantity= '%s' WHERE UserId = '%s' AND ItemId='%s'", order.getItemQuantity(), order.getUserId(), order.getItemId());
    db.execSQL(query);
}

// Function for increasing Cart
public void increaseCart(String uId, String itemId) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("UPDATE OrderDetail SET ItemQuantity= ItemQuantity+1 WHERE UserId = '%s' AND ItemId='%s'", uId, itemId);
    db.execSQL(query);
}

// Function for removing items from the cart
public void removeFromCart(String itemId, String uId) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("DELETE FROM OrderDetail WHERE UserId='%s' and ItemId='%s'", uId, itemId);
    db.execSQL(query);
}

// Function for adding items to Favorites
public void addToFavorites(Favourites item) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("INSERT INTO Favourites(UserId, ItemId, ItemName, ItemPrice, ItemImage, ItemCondition, ItemStyle) " +
                    "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s');",
            item.getUserId(),
            item.getItemId(),
            item.getItemName(),
            item.getItemPrice(),
            item.getItemImage(),
            item.getItemCondition(),
            item.getItemStyle());
    db.execSQL(query);
}

// Function for removing items to Favorites
public void removeFromFavorites(String uId, String itemId) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("DELETE FROM Favourites WHERE UserId='%s' and ItemId='%s';", uId, itemId);
    db.execSQL(query);
}

// Function to know if item is already Favourite
public boolean isFavorite(String uId, String itemId) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("SELECT * FROM Favourites WHERE UserId='%s'and ItemId='%s';", uId, itemId);
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.getCount() <= 0) {
        cursor.close();
        return false;
    }
    cursor.close();
    return true;
}

// Function for getting all items in Favorites
public List<Favourites> getAllFavorites(String uID) {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = {"UserId", "ItemId", "ItemName", "ItemPrice", "ItemImage", "ItemCondition", "ItemStyle"};
    String sqlTable = "Favourites";

    qb.setTables(sqlTable);
    Cursor cursor = qb.query(db, sqlSelect, "UserId=?", new String[] {uID}, null, null, null);

    final List<Favourites> result = new ArrayList<>();

    if (cursor.moveToFirst()) {
        do {
            result.add(
                    new Favourites(
                            cursor.getString(cursor.getColumnIndex("UserId")),
                            cursor.getString(cursor.getColumnIndex("ItemId")),
                            cursor.getString(cursor.getColumnIndex("ItemName")),
                            cursor.getString(cursor.getColumnIndex("ItemPrice")),
                            cursor.getString(cursor.getColumnIndex("ItemImage")),
                            cursor.getString(cursor.getColumnIndex("ItemCondition")),
                            cursor.getString(cursor.getColumnIndex("ItemStyle"))));
        } while (cursor.moveToNext());
    }
    return result;
}

用户将从中将商品添加到购物车的位置。 Item Detail Screen

Cart

购物车下面的值是要设置的默认值,所以请忽略它们:)

1 个答案:

答案 0 :(得分:0)

将检查项放入ColorName,ColorValue和Size的checkItemExists中可以解决此问题。更改此行代码。

public boolean checkItemExists(String itemId, String uId, String mItemColorName, String mItemColorValue, String mItemSize) {
    boolean flag;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor;
    String SQLQuery = String.format("SELECT * FROM OrderDetail WHERE UserId='%s' AND ItemId='%s' AND ItemColorName='%s' AND ItemColorValue='%s' AND ItemSize='%s'", uId, itemId, mItemColorName, mItemColorValue, mItemSize);
    cursor = db.rawQuery(SQLQuery, null);
    flag = cursor.getCount() > 0;
    cursor.close();
    return flag;
}