1

我在此行中收到此异常“绑定或列索引超出范围”:

Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);

我想检查给定的 id 是否存在于数据库中,但查询总是抛出这个异常,我不知道出了什么问题。我试过这个,但它是一样的例外:

Cursor cursor = db.query(TABLE_NAME, null, ID+" = ?", new String[] { id }, null, null, null);

这是我编写的方法。如果这里有什么问题,请告诉我:

public boolean existsAcc(String id)
    {
        //Check the parameters
        if (id == null)
        {
            throw new IllegalArgumentException(
                    "The id parameter cannot be null");
        }
        SQLiteDatabase db = getReadableDatabase();

        Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);

        if (!cursor.moveToFirst())
        {
            cursor.close();
            return false;
        }

        cursor.close();

        return true;
    }
4

1 回答 1

2

我不知道为什么奇拉格删除了他的答案,但他是正确的。改变

Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);

Cursor cursor = db.query(TABLE_NAME, null, ID + " = '" + id + "'", null, null, null, null);

成功了。

于 2011-03-09T13:16:44.973 回答