Tela Inicial Dinamica

Asked

Viewed 96 times

0

I’d like to create something more or less 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, android will open the main activity and check the database, if you have any table, keep the main activity, if you have nothing, it opens Activity for table creation.

If you are able 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’s easy to understand my purpose.

  • 1

    No need to use <br> to make line break. Use the space bar twice and do enter. For more information about formatting follow this link.

  • 1

    Thank you so much for the @ramaral tip

2 answers

0

Could do so 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;
}
  • I am finishing some other parts and will do the tests in your code. Thank you! @EDIT Could put comments on what each code does, some things I have idea of what it does, others not, I started programming on android less than a week ago, to half lost yet.

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 checks if there is a table and its return is boolean and puts the validation in if.

Browser other questions tagged

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