Check whether database is empty

前端 未结 7 1643
遇见更好的自我
遇见更好的自我 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:36

    I know this is too late ,but i had the same error before and order to fix it change your code to:

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

    You have to add while statement not if or else it will cause a IndexOutOfBound exception

提交回复
热议问题