检查数据库是否为空

我正在尝试检查一个sqlite数据库是否为空使用

public boolean chkDB(){
        boolean chk = false;
        Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
        if (mCursor != null){
            mCursor.moveToFirst();
            if (mCursor.getInt(0) == 0){
                chk = false;
            }
        }else{
            chk = true;
        }
        return chk;
    }

但每次我调用该方法时,我都会得到空指针异常

我的Logcat显示了这一点

06-28 22:35:19.519: E/AndroidRuntime(441):  at com.android.id.DBAdapter.chkDB(DBAdapter.java:82)
06-28 22:58:06.269: E/AndroidRuntime(621): FATAL EXCEPTION: main
06-28 22:58:06.269: E/AndroidRuntime(621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.id/com.android.id.MainActivity}: java.lang.NullPointerException
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.os.Looper.loop(Looper.java:123)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 22:58:06.269: E/AndroidRuntime(621):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 22:58:06.269: E/AndroidRuntime(621):  at java.lang.reflect.Method.invoke(Method.java:521)
06-28 22:58:06.269: E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 22:58:06.269: E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 22:58:06.269: E/AndroidRuntime(621):  at dalvik.system.NativeStart.main(Native Method)
06-28 22:58:06.269: E/AndroidRuntime(621): Caused by: java.lang.NullPointerException
06-28 22:58:06.269: E/AndroidRuntime(621):  at com.android.id.DBAdapter.chkDB(DBAdapter.java:82)
06-28 22:58:06.269: E/AndroidRuntime(621):  at com.android.id.MainActivity.enterDB(MainActivity.java:66)
06-28 22:58:06.269: E/AndroidRuntime(621):  at com.android.id.MainActivity.onCreate(MainActivity.java:23)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-28 22:58:06.269: E/AndroidRuntime(621):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-28 22:58:06.269: E/AndroidRuntime(621):  ... 11 more

mCursor.moveToFirst()返回是否成功找到元素的布尔值。 用它移动到游标的第一行,同时检查一行是否真的存在。

Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
Boolean rowExists;

if (mCursor.moveToFirst())
{
   // DO SOMETHING WITH CURSOR
  rowExists = true;

} else
{
   // I AM EMPTY
   rowExists = false;
}

无论是否存在,您都试图访问游标中的一行。


if(mCursor.getCount() == 0) 

应该做的伎俩


if(mCursor!=null&&mCursor.getCount>0)
链接地址: http://www.djcxy.com/p/94447.html

上一篇: Check whether database is empty

下一篇: Delete row if table exists SQL