Database in does not reflect changes in the same activity

4

I'm trying to update the database in my application. Trying to simulate a possible production error intentionally I give DROP to a table, and then I make a SELECT in the same table and it works. When changing Activity the same procedure is performed, but this time the error happens.

The following steps are performed:

  • Load LoadScreenActivity

  • Performs the onUpgrade() , if necessary, which copies the new asset base.

  • If onUpgrade() is called it runs procedimento() to check if the database is corrupted, as I did not find a way to corrupt the database so I can use PRAGMA integrity_check , so I delete the table messages only to simulate the error.

  • I call testar() to check 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 this case the MainActivity ) and do the same SELECT ...

      

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

    Why does not the error immediately happen in the Activity ?

        
    asked by anonymous 16.12.2013 / 17:14

    2 answers

    3

    You are catching the exception in the testar() method and returning false . As a result, your SELECT may be failing but the program execution is not interrupted and no error message is displayed.

        
    16.12.2013 / 17:36
    0

    Possibly it is running SELECT before executing COMMIT in the database and, therefore, takes ghosted data. When it does the update in the application the bank is "comitative", because the application terminates the connection with the bank and does an auto-commit. A database.commit() after DROP should solve the problem.

        
    24.01.2014 / 00:34