Reputation: 6527
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!
Upvotes: 14
Views: 23118
Reputation: 4829
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);
}
}
Upvotes: 5
Reputation: 1168
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);
}
}
Upvotes: 0
Reputation: 14600
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();
}
}
Upvotes: 11