Beginner
Beginner

Reputation: 29543

Android get list of tables

Does anyone know the SQL to get a list of table names via code in Android? I know .tables does it through command shell but this doesn't work through code. Is it anything to do with the meta-data, etc.?

Upvotes: 5

Views: 3773

Answers (2)

Beginner
Beginner

Reputation: 29543

Got it:

SELECT * FROM sqlite_master WHERE type='table'

Upvotes: 3

Giulio Prisco
Giulio Prisco

Reputation: 941

Just had to do the same. This seems to work:

public ArrayList<Object> listTables()
    {
        ArrayList<Object> tableList = new ArrayList<Object>();
        String SQL_GET_ALL_TABLES = "SELECT name FROM " + 
        "sqlite_master WHERE type='table' ORDER BY name"; 
        Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            do {
                tableList.add(cursor.getString(0));
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return tableList;
    }

Upvotes: 10

Related Questions