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

I can't find a way to get / dump table names in text view from a database in android. I know about: request

SELECT * FROM sqlite_master 

The above expression displays a cursor indicating metadata about all databases / views / tables. But how can I get the names of all the tables I created in my database?

+4
source share
3 answers

If you want to get information in an array (or any structure that you can iterate over rows), you can make a method such as:

 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; } 
+2
source

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

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

In addition, if you are just trying to navigate the database, you can simply load it into sqlite3 in the adb shell and run ".tables":

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

All Articles