3

I want to show an alertbox if the table does not exist or the table content is empty.

To check if the table is empty I'm using:

        cCheckDB = db.rawQuery("SELECT COUNT(*) FROM " + MainActivity.TABLE_NAME +"", null);

        if (cCheckDB != null) {
            cCheckDB.moveToFirst();

            if (cCheckDB.getInt (0) == 0) {
                // EMPTY
            }
        }

But how to wrap this in an "if table exist"? What is the best way?

I know both ways to check if a table exist or to check if there is content in it. But I want to use the checks in a meaningful and way and not in single query if this is possible - that's my question.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Oli
  • 3,496
  • 23
  • 32
  • 1
    Please check this flow http://stackoverflow.com/questions/3058909/how-does-one-check-if-a-table-exists-in-an-android-sqlite-database – Volodymyr May 17 '16 at 06:29
  • Sure I can catch the Exception or create if not exist but this is not helpfull in my case. I thought there is another way – Oli May 17 '16 at 06:31

3 Answers3

5

Hope,This will be helpful to you. It's working well!!!

public boolean isTableExists(String tableName, boolean openDb) {
    if(openDb) {
        if(mDatabase == null || !mDatabase.isOpen()) {
            mDatabase = getReadableDatabase();
        }

        if(!mDatabase.isReadOnly()) {
            mDatabase.close();
            mDatabase = getReadableDatabase();
        }
    }

    Cursor cursor = mDatabase.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
    if(cursor!=null) {
        if(cursor.getCount()>0) {
                            cursor.close();
            return true;
        }
                    cursor.close();
    }
    return false;
}
Akash Patel
  • 218
  • 2
  • 10
2

To check that your table exists or not, you can use:

SELECT name FROM sqlite_master WHERE type='table' AND name='table_name';

here, replace table_name with your table name you want to change

EX:

SELECT name FROM sqlite_master WHERE type='table' AND name='Employee_Details';
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

the more optimal way is to check if table exist then check if record exist or not

 private void clearTable(String tableName) {
        int count;
        Cursor cursor = db.rawQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND " +
                "name= " + tableName, null);
        cursor.moveToFirst();
        count = cursor.getInt(0);
        if (!cursor.isClosed())
            cursor.close();

        if (count > 0)//table exist now delete record if it exist
        {
            cursor=db.rawQuery("select exists(select 1 FROM " + tableName + ")", null);
            cursor.moveToFirst();
            count = cursor.getInt(0);
            if (!cursor.isClosed())
                cursor.close();
            if (count > 0)
                db.execSQL("delete from " + tableName);
        }
    }
Pramod mishra
  • 615
  • 6
  • 16