Reputation: 163
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?
Upvotes: 4
Views: 6552
Reputation: 2215
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
Upvotes: 0
Reputation: 7082
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;
}
Upvotes: 2
Reputation: 171411
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;
Upvotes: 2