如何获得在Android的SQLite数据库的所有表名?如何获得、数据库、Android、SQLite

2023-09-06 01:19:36 作者:铲屎大将军

我也试试这个code

I have try this code

 Cursor c=db.rawQuery("SELECT name FROM sqlite_master WHERE type = 'table'",null);

 c.moveToFirst();

 while(!c.isAfterLast()){

Toast.makeText(activityName.this, "Table Name=> "+c.getString(0), Toast.LENGTH_LONG).show();

}

但它会抛出错误android.database.sqlite.SQLiteException:没有这样的表:SQLITE_MASTER(code 1):,在编译:SELECT名字从SQLITE_MASTER其中type ='表'

如何获取所有表名?帮我

How to fetch all table name? Help me

在此先感谢。

推荐答案

经过,测试和运行。试试这个code:

Checked, tested and functioning. Try this code:

Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

if (c.moveToFirst()) {
    while ( !c.isAfterLast() ) {
        Toast.makeText(activityName.this, "Table Name=> "+c.getString(0), Toast.LENGTH_LONG).show();
        c.moveToNext();
    }
}

我假设,在某一时刻的路线,你会抢的的的表名的列表的显示的可能是一个的ListView 或者其他的东西。不只是显示一个面包。

I am assuming, at some point down the line, you will to grab a list of the table names to display in perhaps a ListView or something. Not just show a Toast.

未测试code。究竟是什么来了在我心中的顶部。在生产应用程序中使用它之前做测试。 ; - )的

在这种情况下,可以考虑以下更改code上面贴:

In that event, consider the following changes to the code posted above:

ArrayList<String> arrTblNames = new ArrayList<String>();
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

    if (c.moveToFirst()) {
        while ( !c.isAfterLast() ) {
            arrTblNames.add( c.getString( c.getColumnIndex("name")) );
            c.moveToNext();
        }
    }