SQLite,如何获取数据库中的所有表名?

SQLite,如何获取数据库中的所有表名?

问题描述:

您认为从数据库获取所有表名称的正确方法是什么?将它们添加到列表中?

What do you think would be the right way to get all table names' from a database and add them to a list?

/ p>

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();

但它输出一些android_metadata,而不是我的表名。因此猜测查询有问题。

But it outputs some "android_metadata" instead of my table name's. So guess there's something wrong with the query.

谢谢!

刚刚做了一个快速测试在SQLite Manager插件在FireFox与我使用的SQLite数据库,你使用的查询返回表名。

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();
        }
    }