0

I have an app that use a sqlite database. For every query (10 query selects in 10 differents methods) i open the database. It is a good choice to open db when the application is started and close it when application close? For example, creating an static reference to my DB object in MyApplication class (extends Application).

Thanks for your advices.

1 Answer 1

1

Create a static singleton which keeps references to the db and other oft-used resources, such as this:

class Global {
    private static SQLiteDatabase _db = null;

    public static SQLiteDatabase getDb() {
        if( _db == null ) {
            ... assign & open _db
        }

        return _db;
    }
}

Elsewhere, just reference Global.getDb()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.