Check whether database is empty

前端 未结 7 1633
遇见更好的自我
遇见更好的自我 2020-12-18 23:11

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

public boolean chkDB(){
        boolean chk = false;
        Cursor mCursor = db.rawQuery(\"SELECT          


        
相关标签:
7条回答
  • 2020-12-18 23:56

    You can query database with total row count. If it is > 0 then you can run your next code. Below function will return row value from database.

     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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题