简体   繁体   中英

How to get the names of all tables present in a database in Android SQL lite

I cant find a way to get/dump table names in a text view from a database in android. I know about:querying

SELECT * 
  FROM sqlite_master

The above statement results a cursor pointing the metadata about all databases/view/tables. But how can I fetch the names of all the tables I created, in my database?

Not sure if you are asking for the SQL or Android rendering code, but according to the SQLite FAQ , this SQL should work to give you the names:

SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;

If you want to retrieve the info in an array (or any structure which you can iterate through strings), you can make a method like:

public String[] getDBNames(){
    String[] result;
    try {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT name FROM sqlite_master "); 
        sb.append("WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' ");
        sb.append("UNION ALL ");
        sb.append("SELECT name FROM sqlite_temp_master "); 
        sb.append("WHERE type IN ('table','view') ");
        sb.append("ORDER BY 1");

        Cursor c = _db.rawQuery(sb.toString(), null);
        c.moveToFirst();

        result = new String[c.getCount()];
        int i = 0;
        while (c.moveToNext()) {
            result[i]= c.getString(c.getColumnIndex("name"));
            i++;
        }
        c.close();
    }
    catch(SQLiteException e){
        Log.e("OOPS", e);
    }
    return result;
}

Also, if you're just trying to poke around a database, you can just load it up in sqlite3 in adb shell and run ".tables":

cd data/data/com.whatever.appname/databases
sqlite3 DBNAME
.tables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM