Count the number of tables in the sqlite database

Viewed 16050

I need a count of the tables that are currently in my sqlite database. Tried searching but did not get any direct method.

I tried this method in my DbHelper.

public int countTables() {
    int count = 0;
    String SQL_GET_ALL_TABLES = "SELECT * FROM sqlite_master WHERE type='table'";
    Cursor cursor = getReadableDatabase()
            .rawQuery(SQL_GET_ALL_TABLES, null);
    cursor.moveToFirst();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

        count++;
        getReadableDatabase().close();

    }
    cursor.close();
    return count;
}

But this method gives me a wrong count. I have only 3 tables in my DB, but this method returns count as 5. What could be wrong with this method. Is there any direct method to get the count.

3 Answers
Related