Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all tables of ormlite db android

I want to list all the table names of an ormlite db in android

The code to create a 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);
    }
}
like image 319
karthik Avatar asked Oct 20 '22 05:10

karthik


2 Answers

If I understand you want to list of the tables on the DB right? Pardon me if I am wrong, based on what I understood here is an example.

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;
    }
like image 50
Vlade087 Avatar answered Oct 21 '22 23:10

Vlade087


List all tables of ormlite db android

There is not a method in ORMLite that lists the existing tables but you can certainly use the dao.queryRaw(...) methods to do this. Here are the docs on raw-queries.

Something like the following should work:

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]);
}

This will work on any of your DAO objects since they should connect to the same database.

like image 42
Gray Avatar answered Oct 21 '22 22:10

Gray