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);
}
}
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With