如何测试是否光标是空的SQLiteDatabase查询光标、测试、SQLiteDatabase

2023-09-05 11:02:09 作者:诗化ㄋ丶相逢

我有由下code创建SQL表:

 公共无效的onCreate(SQLiteDatabase DB){
    db.execSQL(CREATE TABLE+ TABLE_NAME +(+ _ID
        +INTEGER PRIMARY KEY AUTOINCREMENT,+主语
        +TEXT NOT NULL,+主题+TEXT NOT NULL,
        + LECTURENUMBER +TEXT NOT NULL,+的PageNumber
        +TEXT NOT NULL,+日期+TEXT NOT NULL,+ _DATA
        +TEXT NOT NULL););
}
 

我查询表如下:

 字符串SQL =SELECT+ _ID +,+主语+FROM+ TABLE_NAME
    +GROUP BY+主语+;

光标光标= subjects.getReadableDatabase()rawQuery(SQL,NULL);
 

现在的问题是我要开始的活性的,如果光标为空(即表中存储任何值)和b活动,如果游标不为空(即表填写)。

我无法找到一种方法,它可以告诉我,如果表是空的或没有。 我曾尝试使用日志如下:

 私人无效showSubjectsOnList(){
    串SQL =选择+ _ID +,+主语+FROM+ TABLE_NAME
        +GROUP BY+主语+;

    光标光标= subjects.getReadableDatabase()rawQuery(SQL,NULL);

    Log.d(事件,Integer.toString(cursor.getCount()));
    如果(cursor.isNull(0)!= FALSE){
        cursor.close();
        subjects.close();
        startActivity(新意图(这一点,OpenScreen.class));
    }
}
 
如何创建sqlite数据库

但日志显示1,如果表是空的......又一次1,如果表中有1项....这表明2,如果表中有两个条目等。

您能否提供解决的基础上,如果游标是空的或不启动不同的活动我的问题的一些方法。

解决方案

怎么样测试光标这样的,然后做你所说的话:

 如果(光标= NULL和放大器;!&安培; cursor.getCount()0)
 

  

getCount将()

返回行的数量在光标

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

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

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