Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite, how to get all table names in database?

What do you think would be the right way to get all table names' from a database and add them to a list?

Right now got that far:

final ArrayList<String> dirArray = new ArrayList<String>();

SqlHelper sqlHelper = new SqlHelper(this, "TK.db", null, 1);
SQLiteDatabase DB = sqlHelper.getWritableDatabase();
Cursor c = DB.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

c.moveToFirst();
while (c.isAfterLast() == false) {
   dirArray.add("n" + c.getColumnIndex("name"));
   c.moveToNext();
 }
c.close();

But it outputs some "android_metadata" instead of my table name's. So guess there's something wrong with the query.

Thanks!

like image 378
Roger Avatar asked Sep 15 '11 12:09

Roger


People also ask

How do I SELECT all tables in a database?

The easiest way to find all tables in SQL is to query the INFORMATION_SCHEMA views. You do this by specifying the information schema, then the “tables” view. Here's an example. SELECT table_name, table_schema, table_type FROM information_schema.

How do I get a list of column names in SQLite?

PRAGMA table_info(table_name); will get you a list of all the column names.


1 Answers

Just did a quick test in the SQLite Manager plugin in FireFox with the SQLite db i'm working with and the query you're using does return the table names. Are you creating the tables correctly and have you tested the exist to your expectations?

To iterate through, do something like:

    if (c.moveToFirst())
    {
        while ( !c.isAfterLast() ){
           dirArray.add( c.getString( c.getColumnIndex("name")) );
           c.moveToNext();
        }
    }
like image 78
SBerg413 Avatar answered Oct 07 '22 04:10

SBerg413