ANDROID SQLITE检查,如果表中有数据 [英] ANDROID SQLITE check if table has data

查看:159
本文介绍了ANDROID SQLITE检查,如果表中有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的表中有记录。以下是我已经试过:

I'd like to check if my table has records. Here's what I've tried:

if( cursor != null ){
        cursor.moveToFirst();
        if (cursor.getInt(0) == 0) {
        Toast.makeText(getBaseContext(), "No records yet!", Toast.LENGTH_SHORT).show();
        }
    }

LogCat中说:

Logcat says:

  CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

任何帮助是AP preciated。谢谢。

Any help is appreciated. Thanks.

推荐答案

试试这个

cursor.getCount();

如果游标检索如果给0数据指望它会返回那么就没有数据

it will return if cursor retrieve data count if this give 0 then there is no data

所以

if(cursor != null && cursor.getCount()>0){
   cursor.moveToFirst();
   //do your action
   //Fetch your data

}
else {
 Toast.makeText(getBaseContext(), "No records yet!", Toast.LENGTH_SHORT).show();
    return;
}  

请注意:即使你检查如果(指针!= NULL)光标做任何的查询后,将不会返回空值,所以做这个检查在光标的数据计数之后

Note : even though you check if( cursor != null ) cursor will not return null value after doing any query, so do this to check the data count in cursor after that

这篇关于ANDROID SQLITE检查,如果表中有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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