List tables with specific column

Asked

Viewed 224 times

1

I am creating an Android application using Sqlite and need to get a list of tables with a specific column as for example:

SELECT table_name FROM sqlite_master WHERE table_column_map = 'imagem'

It’s possible to do that?

  • It is possible to get what you want but not this way since Sqlite does not secure the table data in an easy way to do query. I will not try to answer because essentially it should be done with Java code on Android and I do not know the API to try to mount a code.

1 answer

1

Eventually I built 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;
}

Utilizing:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.