此函数始终返回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;
}
答案 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;
}