SQLite用where子句选择查询

时间:2012-06-24 18:00:03

标签: android select sqlite where

我对SQLite select语句

有以下功能
public Boolean itemFound(String cartId,Long ItemId){
        SQLiteDatabase db = this.getReadableDatabase();
        Log.d("search",cartId+"--"+ItemId);

        try {
            String where = COL_ITEM_CART_ID+"='"+cartId+"' and "+COL_ITEM_ID+" ="+ ItemId+"";
            Log.d("where===============", where);
            String columns[] = new String[] { COL_ITEM_ID,COL_ITEM_NAME, COL_ITEM_PRICE, COL_ITEM_ADDDATE,COL_ITEM_QUANTITY,COL_ITEM_CART_ID }; // > null means * (all)
            Cursor c = db.query(TABLE_NAME,
                                columns ,
                                COL_ITEM_CART_ID+"='?' and "+ COL_ITEM_ID + "=?"  ,
                                new String[]{cartId,String.valueOf(ItemId)}, 
                                null, 
                                null, 
                                COL_ITEM_ADDDATE+" desc");


            int numRows = c.getCount();

            c.close();
            db.close();
            if(numRows>0)
                return true;
            else
                return false;
        } catch (SQLException e) {
            db.close();
            return false;
        }
    }

参数是:

  • Long输入itemId = 9882889921
  • String键入cartId = ca1745ef-24eb-4da8-a1ea-9650d78ba5c0

尽管有这些数据的重新编码。返回的行是0,为什么?

2 个答案:

答案 0 :(得分:0)

您已将?换成单引号,请更改:

COL_ITEM_CART_ID+"='?' and "+ COL_ITEM_ID + "=?"

上面的一行是尝试将COL_ITEM_CART_ID字面上与问号匹配,如下所示进行更改:

COL_ITEM_CART_ID + "=? and "+ COL_ITEM_ID + "=?"

答案 1 :(得分:0)

query方法将为您清理数据输入,因此当您使用撇号包裹?时,撇号将成为输入字符串本身的一部分。这导致您的查询返回0行。

要解决此问题,请删除将'?'的所有实例更改为?