GreenDAO - get all databases and entities

997 Views Asked by At

I have an android app that works with greenDAO, and I'm trying to get all the databases (kind of like show databases in MySQL), to show in a RecyclerView, when the user selects an item then get all the entities inside the database selected an show them in another fragment and then when the user selects an entity then get all the data and show it in another fragment.

Is there a way to get that information with greenDAO? Another way to get it?(sqlite maybe?) or the only way is to show what's already there(in terms of databases and entities) and change it accordingly to how and when the databases and number of entities change?

1

There are 1 best solutions below

0
On

went with an sligthly better version of option 3(show what's alerdy there and change it acordign to how and when the databases and number of entities change) where i created a class that initializes all databases and entities(DaoMaster.DevOpenHelper,SQLiteDatabase and entityNameDao), there, i get the database name and create a list of every entity and pass it to the activity, and from there, get the list of data from the entity with de Dao. not the way i wanted to make it but at least it's not a mess to update.

Edit:ok so after a while i found that you can get all that data with Sqlite, all except all the databases. For the tables or entities you can query SELECT name FROM sqlite_master WHERE type='table', now with all the table names you can query each table, for getting the data you have to first get the atributes with the Cursor.getColumnNames(); and then get them with getColumnIndex(); here's a little example

ArrayList<String> arrTblNames = new ArrayList<>();
    List<List<String>> data = new ArrayList<>();

    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();
        }
    }
    for(int i=0; i<=arrTblNames.size()-1;i++){
        Cursor dbCursor = db.query(arrTblNames.get(i), null, null, null, null, null, null);
        String[] columnNames = dbCursor.getColumnNames();
        Cursor c2 = db.query(arrTblNames.get(i),columnNames,null,null,null,null,null);
        if (c2.moveToFirst()) {
            while ( !c2.isAfterLast() ) {
                data.add(new ArrayList<String>());
                for(int j=0; j<=columnNames.length-1;j++){
                    data.get(i).add( c2.getString( c2.getColumnIndex(columnNames[j])) );
                }
                c2.moveToNext();
            }
        }
    }