没有这样的列:UserPhone(代码1):,而在编译时:从OrderDetail中删除WHERE UserPhone ='+ 59175223813'

时间:2019-10-16 22:48:39

标签: java

我的Database.java中出现此错误,但是我不在哪部分出错。我已经放了removeFromCart(String productId,String userPhone),但是我的电话号码出现错误。我正在使用Facebook帐户工具包进行记录。当我尝试删除项目时,我的应用程序崩溃。请问你能帮帮我吗? Logcat在Database> removeFromCart

中显示了我

Database.java

public List<Order> getCarts()
{
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[]sqlSelect = {"ID","ProductName","ProductID","Quantity","Price","Discount","Image"};
    String sqlTable ="OrderDetail";

    qb.setTables(sqlTable);
    Cursor c = qb.query(db,sqlSelect,null,null,null,null,null);

    final List<Order> result = new ArrayList<>();
    if (c.moveToFirst())
    {
        do{
            result.add(new Order(
                    c.getInt(c.getColumnIndex("ID")),
                    c.getString(c.getColumnIndex("ProductID")),
                    c.getString(c.getColumnIndex("ProductName")),
                    c.getString(c.getColumnIndex("Quantity")),
                    c.getString(c.getColumnIndex("Price")),
                    c.getString(c.getColumnIndex("Discount")),
                    c.getString(c.getColumnIndex("Image"))
            ));
        }while (c.moveToNext());
    }
    return result;
}

public void addToCart(Order order)
{
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("INSERT INTO OrderDetail(ProductID,ProductName,Quantity,Price,Discount,Image) VALUES('%s' , '%s' , '%s' , '%s' , '%s','%s');",
            order.getProductId(),
            order.getProductName(),
            order.getQuantity(),
            order.getPrice(),
            order.getDiscount(),
            order.getImage());
    db.execSQL(query);
}

public void cleanCart(String userPhone) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("DELETE FROM OrderDetail WHERE UserPhone='%s'",userPhone);
    db.execSQL(query);
}

public void removeFromCart(String productId, String userPhone) {
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("DELETE FROM OrderDetail WHERE UserPhone='%s' and ProductId='%s'",userPhone,productId);
    db.execSQL(query);
}

//Favorites

public void addToFavorites(String foodId, String userPhone)
{
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("INSERT INTO Favorites(FoodId,UserPhone) VALUES('%s','%s');",foodId,userPhone);
    db.execSQL(query);
}

public void removeFromFavorites(String foodId, String userPhone)
{
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("DELETE FROM Favorites WHERE FoodId='%s' and UserPhone='%s';",foodId,userPhone);
    db.execSQL(query);
}

public boolean isFavorites(String foodId, String userPhone)
{
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("SELECT * FROM Favorites WHERE FoodId='%s' and UserPhone='%s';",foodId,userPhone);
    Cursor cursor = db.rawQuery(query,null);
    if (cursor.getCount() <= 0)
    {
        cursor.close();
        return false;
    }
    cursor.close();
    return true;
}

Cart.java

@Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle().equals(Common.DELETE))
            deleteCart(item.getOrder());
        return true;
    }

private void deleteCart(int position) {
        //We will remove item at List<Order> by position
        cart.remove(position);
        //After that , we will delete all old data from SQLite
        new Database(this).cleanCart(Common.currentUser.getPhone());
        //And final , we will update new data from List<Order> to SQLite
        for (Order item:cart)
            new Database(this).addToCart(item);
        //Refresh
        loadListFood();
    }

0 个答案:

没有答案