Search code examples
androidactiveandroid

How to get column names with Active Android


With Active Android, can I get the column names of a table as a String[]? RawQuery() returns a List<Model> so I can't just plug PRAGMA into it.

The suggested duplicate is about getting table names, and not related to Active Android.


Solution

  • Use these methods to get a collection of Fields:

    public static synchronized TableInfo Cache.getTableInfo(Class<? extends Model> type);
    public Collection<Field> TableInfo.getFields();
    

    Then use public String TableInfo.getColumnName(Field field) on each Field to get column names. Example:

    TableInfo<> userInfo = Cache.getTableInfo(User.class);
    Collection<Field> fields = userInfo.getFields();
    Set<String> columnNames = new HashSet<>();
    
    for (Filed field : fields) {
        String name = userInfo.getColumnName(field);
        columnNames.add(name);
    }
    

    If you need to get id column name, use:

    public String TableInfo.getIdName();