Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.?

like image 368
Beginner Avatar asked Jan 13 '11 15:01

Beginner


2 Answers

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;
    }
like image 151
Giulio Prisco Avatar answered Oct 24 '22 09:10

Giulio Prisco


Got it:

SELECT * FROM sqlite_master WHERE type='table'
like image 3
Beginner Avatar answered Oct 24 '22 10:10

Beginner