sqlite查询空日期类型,如何在SQLiteDatabase查询中游标为空时测试

I have an SQL table which is created by the following code:

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);");

}

I query the table as follows:

String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME

+ " GROUP BY " + SUBJECT + ";";

Cursor cursor = subjects.getReadableDatabase().rawQuery(sql, null);

The problem is I have to start an Activity A if the cursor is empty(i.e. the table is storing no values) and Activity B if the cursor is not empty(i.e. table is filled).

I am unable to find a method which can tell me if the table is empty or not.

I Have tried to used Log as follows:

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));

}

}

But the LOG shows 1, if the table is empty...and again 1, if table has 1 entry....it shows 2, if table has two entries and so on.

Can you suggest some method of solving my problem of starting different activities based on if cursor is empty or not.

解决方案

What about testing the cursor like this, and then doing what you've said:

if(cursor!=null && cursor.getCount()>0)

getCount ()

Returns the numbers of rows in the cursor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值