Check whether database is empty

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

    mCursor.moveToFirst() Returns a boolean of whether it successfully found an element or not. Use it to move to the first row in the cursor and at the same time check if a row actually exists.

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

    You are trying to access a row in the cursor regardless of whether one exists or not.

提交回复
热议问题