14

I am trying to check if a sqlite database is empty using

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;
    }

but every time i call that method i get null pointer exception

My Logcat shows this

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
4

7 回答 7

16

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;
}

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

于 2012-06-28T20:18:31.003 回答
10
if(mCursor.getCount() == 0) 

应该做的伎俩

于 2012-06-28T19:54:59.290 回答
0

我知道这为时已晚,但我之前遇到了同样的错误,为了修复它,将您的代码更改为:

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

}
           
        return chk;
    }

您必须添加while语句 not if 否则会导致IndexOutOfBound异常

于 2020-06-22T15:37:58.590 回答
0
if(mCursor!=null&&mCursor.getCount>0)
于 2017-11-03T06:14:17.347 回答
0

您可以使用总行数查询数据库。如果它 > 0 那么你可以运行你的下一个代码。下面的函数将从数据库返回行值。

 public BigDecimal getTotalRecordCount() {
    SQLiteDatabase db = getReadableDatabase();

    String sql = "select count(*) from " + DatabaseHelperTable.TABLE_NAME ;

    Cursor cursor = db.rawQuery(sql, null);

    try {
        cursor.moveToFirst();
        String sum = cursor.getString(0);

        if (sum == null)
            return new BigDecimal("0");
        return new BigDecimal(sum);
    } finally {
        cursor.close();
    }
}
于 2019-08-28T10:55:15.827 回答
0

设置查询方法(直接在您的 ContentProvider 中)或在另一个类中使用您的 ContentResolver 和一列的投影(ID 应该可以解决问题)。然后查看光标是否包含任何内容。

我在任务类中的 ContentProvider 之外执行了此操作:

//Is database empty?
public static boolean isDbEmpty(Context context) {
    ContentResolver contentResolver = context.getContentResolver();

    String[] projection = new String[] {#_ID#};

    Cursor csr = checkResolver.query(#CONTENT_URI#, projection, null,
            null, null);
    if (csr != null && csr.moveToFirst()) {
        csr.close();
        return  false;
    } else {
        return true;
    }
}
于 2018-01-20T17:56:34.753 回答
0

在 Kotlin 中,你可以做到这一点

package myapp.package.name
import android.content.Context

class SQLiteDatabaseCrud(context: Context) {
    private val dbHelper: DBHelper = DBHelper(context)
    private var chk = false
    fun isEmpty(): Boolean? {
        val db = dbHelper.readableDatabase
        val cursor = db.rawQuery("SELECT * FROM " + Business.TABLE, null)
        chk = if (cursor != null){
            cursor.moveToFirst()
            cursor.count != 0
        }else{
            true
        }
        cursor.close()
        return chk
    }
}

因此,在您的活动中,您只需调用该函数

 private var mSQLiteDatabaseCrud: SQLiteDatabaseCrud? = null

 mSQLiteDatabaseCrud = SQLiteDatabaseCrud(applicationContext)
    if(mSQLiteDatabaseCrud?.isEmpty()!!){
        Toast.makeText(applicationContext,"database is not empty", Toast.LENGTH_SHORT).show()       
    }else{
        performRequest()
        Toast.makeText(applicationContext,"empty database", Toast.LENGTH_SHORT).show()
    }
于 2020-06-10T16:03:25.280 回答