如何在 SQLite 数据库查询中测试游标是否为空
2022-09-01 19:39:58
我有一个由以下代码创建的SQL表:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + SUBJECT
+ " TEXT NOT NULL," + TOPIC + " TEXT NOT NULL, "
+ LECTURENUMBER + " TEXT NOT NULL, " + PAGENUMBER
+ " TEXT NOT NULL, " + DATE + " TEXT NOT NULL, " + _DATA
+ " TEXT NOT NULL);");
}
我按如下方式查询该表:
String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME
+ " GROUP BY " + SUBJECT + ";";
Cursor cursor = subjects.getReadableDatabase().rawQuery(sql, null);
问题是,如果游标为空(即表未存储任何值),我必须启动活动A,如果游标不为空(即表已填充),则必须启动活动B。
我无法找到一个可以告诉我表是否为空的方法。我尝试按如下方式使用日志:
private void showSubjectsOnList() {
String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME
+ " GROUP BY " + SUBJECT + ";";
Cursor cursor = subjects.getReadableDatabase().rawQuery(sql, null);
Log.d("Events",Integer.toString(cursor.getCount()));
if(cursor.isNull(0)!=false){
cursor.close();
subjects.close();
startActivity(new Intent(this,OpenScreen.class));
}
}
但是 LOG 显示 1,如果表是空的...以及 1,如果表有 1 个条目....它显示 2,如果表有两个条目,依此类推。
你能建议一些方法来解决我的问题,根据游标是否为空来启动不同的活动。