mercredi 9 mars 2016

Android: Checking for sqlite table existence always returns true

I have this method which checks whether a table exists in that database or not.However it always returns true.

public boolean checkDomainTableExists(String domain){

        if (database==null||!database.isOpen()){
            database=dBHelper.getReadableDatabase();
        }
        if (!database.isReadOnly()){
            database.close();
            database=dBHelper.getReadableDatabase();
        }

        try{
            cursor=database.rawQuery("SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?", new String[] {"table", domain});

        }catch (Exception e){
                Log.d("exception",e.getLocalizedMessage());
        }
        if (cursor!=null){
            if (cursor.getCount()>0){
                cursor.close();
                return true;
            }
            cursor.close();
        }

        return false;
    }

While I am debugging, I see the cursor count as -1 by the time it reaches to if (cursor.getCount()>0){ and cursor count become 1 when I step over the breakpoint resulting in method return true.

Whatever the tablename I give, this method always return true.

Here are the screenshots for debug console: enter image description here

At this point the cursor count is -1, so I believe it shouldn't get into the loop, but it does and the count goes to 1.

enter image description here

Where am I doing wrong ?

Aucun commentaire:

Enregistrer un commentaire