简体   繁体   English

如何获取android sqlite数据库中的所有表名?

[英]How to get all table names in android sqlite database?

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

But it throws the error:但它抛出错误:

"android.database.sqlite.SQLiteException: no such table: sqlite_master(code 1):, while 
compiling: SELECT name FROM sqlite_master WHERE type='table'"

How to fetch all the table names?如何获取所有表名?

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

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.我假设,在某个时候,您将获取一个表名称列表,以显示在ListView或其他东西中。 Not just show a Toast.不只是展示一个 Toast。

Untested code.未经测试的代码。 Just what came at the top of my mind.正是我脑海中浮现的。 Do test before using it in a production app.在生产应用程序中使用它之前进行测试。 ;-) ;-)

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

将您的 sql 字符串更改为:

"SELECT name FROM sqlite_master WHERE type='table' AND name!='android_metadata' order by name"

To get table name with list of all column of that table使用该表的所有列列表获取表名

    public void getDatabaseStructure(SQLiteDatabase db) {

            Cursor c = db.rawQuery(
                    "SELECT name FROM sqlite_master WHERE type='table'", null);
            ArrayList<String[]> result = new ArrayList<String[]>();
            int i = 0;
            result.add(c.getColumnNames());
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                String[] temp = new String[c.getColumnCount()];
                for (i = 0; i < temp.length; i++) {
                    temp[i] = c.getString(i);
                    System.out.println("TABLE - "+temp[i]);


                    Cursor c1 = db.rawQuery(
                            "SELECT * FROM "+temp[i], null);
                    c1.moveToFirst();
                    String[] COLUMNS = c1.getColumnNames();
                    for(int j=0;j<COLUMNS.length;j++){
                        c1.move(j);
                        System.out.println("    COLUMN - "+COLUMNS[j]);
                    }
                }
                result.add(temp);
            }
}

Try adding the schema before the table尝试在表之前添加架构

schema.sqlite_master schema.sqlite_master

From SQL FAQ来自SQL 常见问题解答

If you are running the sqlite3 command-line access program you can type ".tables" to get a list of all tables.如果您正在运行 sqlite3 命令行访问程序,您可以键入“.tables”以获取所有表的列表。 Or you can type ".schema" to see the complete database schema including all tables and indices.或者您可以键入“.schema”以查看完整的数据库架构,包括所有表和索引。 Either of these commands can be followed by a LIKE pattern that will restrict the tables that are displayed.这些命令中的任何一个都可以跟一个 LIKE 模式,该模式将限制显示的表。

From within a C/C++ program (or a script using Tcl/Ruby/Perl/Python bindings) you can get access to table and index names by doing a SELECT on a special table named "SQLITE_MASTER".在 C/C++ 程序(或使用 Tcl/Ruby/Perl/Python 绑定的脚本)中,您可以通过对名为“SQLITE_MASTER”的特殊表执行 SELECT 来访问表和索引名称。 Every SQLite database has an SQLITE_MASTER table that defines the schema for the database .每个 SQLite 数据库都有一个SQLITE_MASTER 表,用于定义数据库的架构 The SQLITE_MASTER table looks like this: SQLITE_MASTER 表如下所示:

CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT );创建表 sqlite_master (类型文本,名称文本,tbl_name 文本,rootpage 整数,sql 文本);

试试这个:

SELECT name FROM sqlite_master WHERE type = "table";

I tested Siddharth Lele answer with Kotlin and Room, and it works as well.我用 Kotlin 和 Room 测试了 Siddharth Lele 的答案,它也有效。

The same code but using Kotlin and Room is something like that:相同的代码但使用 Kotlin 和 Room 是这样的:

val cursor = roomDatabaseInstance.query(SimpleSQLiteQuery("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('android_metadata', 'sqlite_sequence', 'room_master_table')"))
val tableNames = arrayListOf<String>()
if(cursor.moveToFirst()) {
   while (!cursor.isAfterLast) {
      tableNames.add(cursor.getString(0))
      cursor.moveToNext()
   }
}

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

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