如何在SQLiteDatabase查询中游标为空时测试 [英] How To Test If Cursor Is Empty in a SQLiteDatabase Query

查看:248
本文介绍了如何在SQLiteDatabase查询中游标为空时测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL表,由以下代码创建:

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

我查询表如下:

String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME
    + " GROUP BY " + SUBJECT + ";";

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

问题是如果游标是空的,我必须启动一个活动A没有值)和活动B,如果游标不为空(即表填充)。

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

我找不到一个方法,可以告诉我如果表是空的或不。
我试图使用Log如下:

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

但LOG显示1,如果表为空...再次1,如果表有1个条目....它显示2,如果表有两个条目等。

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

getCount ()

http://developer.android.com/reference/android/database/Cursor.html#getCount()

这篇关于如何在SQLiteDatabase查询中游标为空时测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆