In my android application, I never checked db != null , while the queries work. I read what to check, for example, as in the following code.

 SQLiteDatabase db = new ContactDbHelper( getApplicationContext()).getWritableDatabase(); if (db != null) { Toast.makeText(getApplicationContext(), "DB Contacts is created", Toast.LENGTH_LONG).show(); }else { Toast.makeText(getApplicationContext(), "Error create database!", Toast.LENGTH_LONG).show(); } 

The question is, do I need to check SQLiteDatabase db for null ? This is just a safety net and if so, for what reasons can the database not be created, provided that the code is working?

    1 answer 1

    The database opening code searches for a database along the path specified by the Context.getDatabasePath() method. Context.getDatabasePath() can easily happen that some scum one day decides to overload this method in his Activity and specify the path to external media - then an easy Überraschung will wait for you :)

    So it is necessary to check all the same.

    • It is interesting of course, but I do not catch it a little. Is the database not autonomous for each application (not involving distribution)? What relation can someone's activation have to the database, for example, my application? - TimurVI
    • It means that you yourself or a member of your team in your app can change getDatabasePath() - Barmaley