Database on does not reflect changes in the same Activity

Asked

Viewed 313 times

4

I am trying to update the database in my application. Trying to simulate a possible error in production intentionally I give a DROP in a table, and soon after I do a SELECT in the same table and works. When changing Activity the same procedure is performed, but this time the error happens.

The following steps are carried out:

  1. Carries the LoadScreenActivity

  2. Effectuates the onUpgrade(), if necessary, which copies the new Assets base.

  3. Case onUpgrade() be called if the procedimento() to check if the database is corrupted, as I did not find a way to corrupt the database to use the PRAGMA integrity_check, then I delete the table messages just to simulate the error.

  4. I call the testar() to verify the integrity of the database.

    public boolean procedimento() {
        (...)
        database.execSQL("DROP TABLE mensagens");
        return testar();    
    }
    
    public boolean testar() {
        try {
            database.rawQuery("SELECT COUNT(*) FROM mensagens ORDER BY RANDOM()", new String[]{});
            return true;
        catch(Exception e){
            return false;
        }
    }
    

When I start the next Activity (in case the MainActivity) and I do the same SELECT...

Caused by: android.database.sqlite.Sqliteexception: no such table: messages: while compiling: SELECT COUNT(*) FROM messages ORDER BY RANDOM()

Why the error does not happen immediately in the same Activity?

2 answers

3

You are capturing the exception in the method testar() and returning false. With that, your SELECT may be crashing but program execution is not interrupted and no error message is displayed.

  • sorry, but the example is not complete! the business is that NEVER error, never falls in Exception.

  • This code is executed within onUpgrade()? Seems to be a problem with transactions.

  • During, but not inside. It’s Running right after you do onUpgrade()

  • 1

    Sorry, but for me it became vague whether it is running within onUpgrade or not. Please clarify, preferably including the relevant section of onUpgrade() and the passage that calls procedimento().

  • I edited the text @Piovezan see if it was clearer. Thanks!

  • I know it must be the same, but show the code of the second select... you call testar() again?

Show 1 more comment

0

Maybe he’s running the SELECT before performing the COMMIT in the database, so it picks up ghost data. When it updates the application the database is "commited" because the application terminates the connection to the database and makes an auto-commit. An database.commit() after the DROP should solve the problem.

  • 1

    DROP TABLE is a DDL command and does not require commit().

Browser other questions tagged

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