SQlite android查询中的LIKE语句错误

时间:2012-12-07 15:08:40

标签: android sqlite

我在sqlite android中遇到了LIKE语句的问题。我花了一个小时才搞清楚,但我无法得到错误部分。谁知道我的错误在哪里?

    public Cursor getSearch(String id) {

        String[] args = { id };


        return (database.rawQuery("SELECT " + SQLiteHelper.product_id
                + " as _id," 
                + SQLiteHelper.productName + " ," +SQLiteHelper.productDesp
                + "," + SQLiteHelper.productQtty + ","
                +SQLiteHelper.product_CategoryF+" FROM " 
                + SQLiteHelper.productTable+"  WHERE "
                + SQLiteHelper.productName +" LIKE 'id%'",null));
}

错误消息

 bind or column index out of range: handle 0x5e3ec0

任何人都知道我的错误在哪里?

解决方案

return (database.rawQuery("SELECT " + SQLiteHelper.product_id
                + " as _id," 
                + SQLiteHelper.productName + " ," +SQLiteHelper.productDesp
                + "," + SQLiteHelper.productQtty + ","
                +SQLiteHelper.product_CategoryF+" FROM " 
                + SQLiteHelper.productTable+"  WHERE "
                + SQLiteHelper.productName +" LIKE '"+id+"%'",null));

1 个答案:

答案 0 :(得分:2)

您不需要像那样形成SQL。 rawQuery可以很容易地绑定参数。

rawQuery("SELECT ? as _id, ?, ?, ?, ? FROM ? WHERE ? LIKE 'id%'", new String[] {SQLiteHelper.product_id, SQLiteHelper.productName, SQLiteHelper.productDesp, SQLiteHelper.productQtty, SQLiteHelper.product_CategoryF, SQLiteHelper.productTable, SQLiteHelper.productName});

就您的代码而言,请注意您的CategoryF行上的,"+应为+",

    return (database.rawQuery("SELECT " + SQLiteHelper.product_id
            + " as _id," 
            + SQLiteHelper.productName + " ," +SQLiteHelper.productDesp
            + "," + SQLiteHelper.productQtty + ","
            ,"+SQLiteHelper.product_CategoryF+" FROM " 
            + SQLiteHelper.productTable+"  WHERE "
            + SQLiteHelper.productName +" LIKE 'id%'",null));