列出 ormlite 数据库的所有表 android

List all tables of ormlite db android

我想列出 android

中 ormlite 数据库的所有 table 名称

创建table的代码:

public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    try {
        Log.i(DatabaseHelper.class.getName(), "onCreate");
        TableUtils.createTable(connectionSource, Dummy.class);
    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
        throw new RuntimeException(e);
    }
}

如果我理解你想要列出数据库中的表,对吧? 根据我的理解,如果我错了请原谅我 这是一个例子。

public List<String> getTablesOnDataBase(SQLiteDatabase db){
        Cursor c = null; 
        List<String> tables = new ArrayList<String>();
        try{
            c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
            if (c.moveToFirst()) {
                while ( !c.isAfterLast() ) {
                    tables.add(c.getString(0));
                    c.moveToNext();
                }
            }
        }
        catch(Throwable throwable){
            //Issue reading the SQLite Master table
        }
        finally{
            if(c!=null)
              c.close();
        }
        return tables;
    }

List all tables of ormlite db android

ORMLite 中没有列出现有表的方法,但您当然可以使用 dao.queryRaw(...) 方法来做到这一点。这是 docs on raw-queries.

像下面这样的东西应该可以工作:

GenericRawResults<String[]> results =
    dao.queryRaw("SELECT name FROM sqlite_master WHERE type = 'table'");
for (String[] result : results) {
    System.out.println("One table is: " + result[0]);
}

这将适用于您的任何 DAO 对象,因为它们应该连接到同一个数据库。