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 outp开发者_Go百科uts some "android_metadata" instead of my table name's. So guess there's something wrong with the query.
Thanks!
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();
}
}
try this code
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);
while(c.moveToNext()){
String s = c.getString(0);
if(s.equals("android_metadata"))
{
//System.out.println("Get Metadata");
continue;
}
else
{
dirArray.add(s);
}
}
You may use this one also
Cursor cursor = DB.getInstance(this).getAllTableNames();
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
String tableName = cursor.getString(cursor.getColumnIndex("name"));
Log.i(LOG_TAG, "..." + tableName);
}
}
精彩评论