Home Screens Dynamic

0

I would like to create something like this:

Se(O banco de dados tiver tabela criada) {  
    Manter a Activity Principal;  
} Senão {  
    Abra Activity de criação da tabela;  
}

In short, the android will open the main Activity and check the database, if it has any table, hold the main Activity, if it has nothing, it opens Activity for the creation of the table.

If you have to check the database before opening any Activity with some Splash Screens, for example a screen with a progress bar, it can be. It's even cooler in my opinion. I think it was easy to understand my purpose.

    
asked by anonymous 14.03.2017 / 04:32

2 answers

0

To check if a database exists you can do as follows

public static boolean doesDatabaseExist(ContextWrapper context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();

}

To check if the table exists you can create a database method in which it checks whether the table exists and its return is Boolean and places the validation in the if.

    
14.03.2017 / 12:14
0

I could do this to check if the table exists

public boolean existeTabela(String tableName, boolean openDb) {
    if(openDb) {
        if(myDataBase == null || !myDataBase.isOpen()) {
            myDataBase = getReadableDatabase();
        }

        if(!myDataBase.isReadOnly()) {
            myDataBase.close();
            myDataBase = getReadableDatabase();
        }
    }

    Cursor cursor = myDataBase.rawQuery("select DISTINCT table_name from sqlite_master where table_name = '"+tableName+"'", null);
    if(cursor!=null) {
        if(cursor.getCount()>0) {
            cursor.close();
            return true;
        }
        cursor.close();
    }
    return false;
}
    
14.03.2017 / 15:30