如何测试SQLiteDatabase查询中的游标是否为空

时间:2022-08-14 22:54:36

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

我有一个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);");
}

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

问题是,如果游标是空的,我必须启动一个活动A。如果游标不是空的(例如:表都是)。

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:

我找不到一种方法可以告诉我该表是否为空。我尝试使用Log如下:

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.

但是日志显示的是1,如果表是空的…1,如果表有1项....它显示2,if表有两个条目,以此类推。

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

你能提出一些方法来解决我的问题吗?根据光标是否为空来开始不同的活动。

6 个解决方案

#1


55  

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

像这样测试游标,然后做你说过的事情:

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

getCount ()

getCount()

Returns the numbers of rows in the cursor

返回游标中的行数

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

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

#2


22  

The easiest and cleanest way to test for an empty cursor is the following code:

测试空游标的最简单、最干净的方法是以下代码:

if ( cursor.moveToFirst() ) {
    // start activity a
} else {
    // start activity b
}

Per the docs, the method returns false if the cursor is empty:

根据文档,如果光标为空,方法返回false:

http://developer.android.com/reference/android/database/Cursor.html#moveToFirst%28%29

http://developer.android.com/reference/android/database/Cursor.html moveToFirst % 29 28%

public abstract boolean moveToFirst ()

公共抽象布尔moveToFirst ()

Added in API level 1 Move the cursor to the first row.

添加到API level 1中,将光标移到第一行。

This method will return false if the cursor is empty.

如果游标为空,此方法将返回false。

Returns whether the move succeeded.

返回移动是否成功。

#3


2  

Deleted records remain in SQLite as null records, but getCount() counts only not null records. If your table has some records that are null, some of not null records will have _Id numbers bigger than result of getCount(). To reach them, you can iterate cursor ( using for() loop ) double the number of times than result of getCount() and use the cursor to fill record_Id numbers into an array. Lets say resulting array is { 1, 2, 5, 6, 7, 8, 9, 11, 12, 14 }.

被删除的记录仍然作为空记录保存在SQLite中,但是getCount()只计算不为空记录。如果表中有些记录是空的,那么有些非空记录的_Id号将大于getCount()的结果。要到达它们,可以迭代游标(使用for()循环),使getCount()的结果的次数增加一倍,并使用游标将record_Id号填充到数组中。假设结果数组为{1,2,5,6,7,8,9,11,12,14}。

That means records 3, 4, 10, 13, are null records and your table has 14 record all together, not 10 that you got from getCount().

这意味着记录3、4、10、13都是空记录,您的表总共有14条记录,而不是getCount()中的10条。

Remember:

记住:

  1. getCount() returns number of not null records ,
  2. getCount()返回非空记录的数量,
  3. cursor returns _Id numbers of not null records,
  4. 光标返回非空记录的_Id号,
  5. _Id numbers "missed" by cursor are _Id numbers of null records,
  6. 被光标“漏掉”的id号是空记录的_Id号,
  7. must reach sufficiently further than getCount() to get them all.
  8. 必须达到比getCount()足够远的范围才能获得所有这些。

#4


1  

You just need to use getCount(). If your sql is correct but doesn't return any row you will have a NOT null cursor object but without a rows and getCount() will return 0.

您只需要使用getCount()。如果您的sql是正确的,但是没有返回任何行,那么您将拥有一个NOT null游标对象,但是没有行,getCount()将返回0。

#5


0  

My suggestion would be using a ListActivity.

我的建议是使用ListActivity。

Those are Activity's which are meant to display items in a ListView. You can simply use a SimpleCursorAdapter to populate them (also illustrated in the ListActivitys JavaDoc page).

这些是活动的,目的是在ListView中显示项目。您可以简单地使用一个SimpleCursorAdapter来填充它们(也可以在ListActivitys JavaDoc页面中演示)。

They also offer a setEmptyView()-method, which can be used to display a View (might be a TextView) which informs the user that there are no records yet and how he can create one.

它们还提供了setEmptyView()方法,该方法可用于显示视图(可能是TextView),该视图通知用户没有记录,以及如何创建记录。

An example on how to do that can be found here.

如何做到这一点的一个例子可以在这里找到。

#6


0  

I believe your problem is you're not creating a proper query.

我认为您的问题是您没有创建一个合适的查询。

You should use the SQLiteDatabase query.

您应该使用SQLiteDatabase查询。

Cursor c = db.query(TABLE_NAME, null,
            null, null, null, null, null);

You then can use c.getCount() to determine if the table has anything.

然后可以使用c.getCount()来确定该表是否包含任何内容。

#1


55  

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

像这样测试游标,然后做你说过的事情:

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

getCount ()

getCount()

Returns the numbers of rows in the cursor

返回游标中的行数

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

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

#2


22  

The easiest and cleanest way to test for an empty cursor is the following code:

测试空游标的最简单、最干净的方法是以下代码:

if ( cursor.moveToFirst() ) {
    // start activity a
} else {
    // start activity b
}

Per the docs, the method returns false if the cursor is empty:

根据文档,如果光标为空,方法返回false:

http://developer.android.com/reference/android/database/Cursor.html#moveToFirst%28%29

http://developer.android.com/reference/android/database/Cursor.html moveToFirst % 29 28%

public abstract boolean moveToFirst ()

公共抽象布尔moveToFirst ()

Added in API level 1 Move the cursor to the first row.

添加到API level 1中,将光标移到第一行。

This method will return false if the cursor is empty.

如果游标为空,此方法将返回false。

Returns whether the move succeeded.

返回移动是否成功。

#3


2  

Deleted records remain in SQLite as null records, but getCount() counts only not null records. If your table has some records that are null, some of not null records will have _Id numbers bigger than result of getCount(). To reach them, you can iterate cursor ( using for() loop ) double the number of times than result of getCount() and use the cursor to fill record_Id numbers into an array. Lets say resulting array is { 1, 2, 5, 6, 7, 8, 9, 11, 12, 14 }.

被删除的记录仍然作为空记录保存在SQLite中,但是getCount()只计算不为空记录。如果表中有些记录是空的,那么有些非空记录的_Id号将大于getCount()的结果。要到达它们,可以迭代游标(使用for()循环),使getCount()的结果的次数增加一倍,并使用游标将record_Id号填充到数组中。假设结果数组为{1,2,5,6,7,8,9,11,12,14}。

That means records 3, 4, 10, 13, are null records and your table has 14 record all together, not 10 that you got from getCount().

这意味着记录3、4、10、13都是空记录,您的表总共有14条记录,而不是getCount()中的10条。

Remember:

记住:

  1. getCount() returns number of not null records ,
  2. getCount()返回非空记录的数量,
  3. cursor returns _Id numbers of not null records,
  4. 光标返回非空记录的_Id号,
  5. _Id numbers "missed" by cursor are _Id numbers of null records,
  6. 被光标“漏掉”的id号是空记录的_Id号,
  7. must reach sufficiently further than getCount() to get them all.
  8. 必须达到比getCount()足够远的范围才能获得所有这些。

#4


1  

You just need to use getCount(). If your sql is correct but doesn't return any row you will have a NOT null cursor object but without a rows and getCount() will return 0.

您只需要使用getCount()。如果您的sql是正确的,但是没有返回任何行,那么您将拥有一个NOT null游标对象,但是没有行,getCount()将返回0。

#5


0  

My suggestion would be using a ListActivity.

我的建议是使用ListActivity。

Those are Activity's which are meant to display items in a ListView. You can simply use a SimpleCursorAdapter to populate them (also illustrated in the ListActivitys JavaDoc page).

这些是活动的,目的是在ListView中显示项目。您可以简单地使用一个SimpleCursorAdapter来填充它们(也可以在ListActivitys JavaDoc页面中演示)。

They also offer a setEmptyView()-method, which can be used to display a View (might be a TextView) which informs the user that there are no records yet and how he can create one.

它们还提供了setEmptyView()方法,该方法可用于显示视图(可能是TextView),该视图通知用户没有记录,以及如何创建记录。

An example on how to do that can be found here.

如何做到这一点的一个例子可以在这里找到。

#6


0  

I believe your problem is you're not creating a proper query.

我认为您的问题是您没有创建一个合适的查询。

You should use the SQLiteDatabase query.

您应该使用SQLiteDatabase查询。

Cursor c = db.query(TABLE_NAME, null,
            null, null, null, null, null);

You then can use c.getCount() to determine if the table has anything.

然后可以使用c.getCount()来确定该表是否包含任何内容。