简体   繁体   English

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

[英]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 outputs some "android_metadata" instead of my table name's. 但它输出一些“android_metadata”而不是我的表名。 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. 刚刚使用我正在使用的SQLite数据库在FireFox中的SQLite Manager插件中进行了快速测试,并且您正在使用的查询确实返回了表名。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM