List tables with specific column

1

I'm creating an application on Android using SQLite and I need to get a list of tables with a specific column like:

SELECT table_name FROM sqlite_master WHERE table_column_map = 'imagem'

Is it possible to do this?

    
asked by anonymous 19.01.2015 / 00:04

1 answer

1

I ended up building this function to solve my problem:

public ArrayList<Object> getTablesWhereColumns(String[] columns) {
    ArrayList<Object> tables = getTables();
    ArrayList<Object> requestedTables = new ArrayList<Object>();

    for (Object table : tables){
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM "+table.toString()+" LIMIT 1", null);
        String[] columnNames = cursor.getColumnNames();

        for (String columnName : columnNames) {
            if (Arrays.asList(columns).contains(columnName)) {
                requestedTables.add(table.toString());
                break;
            }
        }
    }

    return requestedTables;
}

Usage:

ArrayList<Object> tables = crud.getTablesWhereColumns(new String[]{"image", "video", "video_image"});
    
19.01.2015 / 14:18