SQlite android 查询中的 LIKE 语句错误



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

你不需要像这样形成你的 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));

最新更新