SQLlite:选择where子句不起作用的查询

时间:2016-04-13 07:44:57

标签: android sqlite android-sqlite where-clause

此函数始终返回false并且游标始终为空,即使数据库中有10条记录,我尝试使用分配ID查询每条记录但不起作用。

我已将数据库从虚拟设备复制到我的电脑中,并使用sqlite manger进行相同的查询,并在PC中工作,数据库中有所有记录。

这是我的代码:

public boolean checkAssignment(String assignmentId) {
        boolean assignmentStored = false;
        String selectQuery = "SELECT * FROM " + TABLE_ASSIGNMENTS +
                " WHERE " + KEY_ASSIGNMENT_ID + " = '" + assignmentId.trim()+"'";
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.getCount() > 0) {
            assignmentStored = true;
        }
        cursor.close();
        return assignmentStored;
}

2 个答案:

答案 0 :(得分:0)

根据您发布的图片,我可以看到assignmentId列的数据类型为数字。

因此请更改您的查询:

String selectQuery = "SELECT * FROM " + TABLE_ASSIGNMENTS +
     " WHERE " + KEY_ASSIGNMENT_ID + " = " + Integer.parseInt(assignmentId.trim());

答案 1 :(得分:0)

以这种方式使用:

public boolean checkAssignment(String assignmentId) {
        boolean assignmentStored = false;    
        String[] columnsToReturn = { "KEY_ASSIGNMENT_ID"};
        String selection = "KEY_ASSIGNMENT_ID =?";
        String[] selectionArgs = { Integer.ParseInt(assignmentId.trim())}; 

        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor =  db.query(TABLE_ASSIGNMENTS, columnsToReturn, selection, selectionArgs, null, null, null);

        if (cursor.getCount() > 0) {
            assignmentStored = true;
        }
        cursor.close();
        return assignmentStored;
    }