-1

我在 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));
4

1 回答 1

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));
于 2012-12-07T15:15:09.957 回答