Check whether database is empty

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

    Set up a query-method (either in your ContentProvider directly) or in another class using your ContentResolver with a projection for one column (ID should do the trick). Then see if the cursor contains anything or not.

    I did this outside the ContentProvider in a task class:

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

提交回复
热议问题