androidSQLite检测表是否有数据

如题,我想检测表是不是有数据,代码如下:

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

logcat如下:

CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

请指教,谢谢。

试试:

cursor.getCount();

如果指针检索到数据就会返回,如果返回0就是没有数据

因此:

if(cursor != null && cursor.getCount()==0){
   Toast.makeText(getBaseContext(), "No records yet!", Toast.LENGTH_SHORT).show();
}
if(cursor.moveToFirst()){
//do your work
}
else{
 Toast.makeText(getBaseContext(), "No records yet!", Toast.LENGTH_SHORT).show();
}

希望对你有用。