如标题所示,我使用以下方法检查我的SQL数据库:
public String[] getRecord(String category){
String[] record = new String[3];
Cursor crsRecord = sqliteDatabase.rawQuery(QUERY_GET_RECORD + category, null);
int i=0;
while(crsRecord.moveToNext()){
record[i] = crsRecord.getString(0);
i++;
}
return record;
}
现在可能就是这一行:
Cursor crsRecord = sqliteDatabase.rawQuery(QUERY_GET_RECORD + category, null);
没有结果,因为我的数据库中没有适当的数据。如何检查我没有结果?
答案 0 :(得分:2)
cursor.moveToFirst();
if (cursor.isAfterLast()){
// You have no results
}
或者,您可以将代码更改为:
while(!crsRecord.isAfterLast()){
// Instead of using an int literal to get the colum index,
// use the getColumnIndex method
int index = crsRecord.getColumnIndex(COLUMN_NAME);
if (index == -1) {
// You don't have the column-- do whatever you need to do.
}
else {
record[i] = crsRecord.getString(index);
i++;
}
crsRecord.moveToNext();
}
如果没有记录,则while循环永远不会启动。