Neetesh
Neetesh

Reputation: 41

Android database openOrCreate() error

I have two classes, both are not activity, one is to perform database operation and one is to forward the values as mediator class. DataBase class calling openOrCreateDataBase method from a 3rd Connectivity class.

    contextWrapper.openOrCreateDatabase(sqlDBName, MODE_PRIVATE, null);

but here since this class is not activity, i am unable to pass parameter of ContextWrapper. Is there any other way to open database. I have tried,

    sqLiteDatabase = SQLiteDatabase.openDatabase(path, factory, flags);
    sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(file, factory);

But these not working for me.

thanks

Upvotes: 0

Views: 1415

Answers (2)

Oz Radiano
Oz Radiano

Reputation: 789

Yes there is a way. In the constructor of the db wrapper class you can add the ContextWrapper as a parameter and call it, like this:

public ctor(ContextWrapper wrapper) {
        SQLiteDatabase db = wrapper.openOrCreateDatabase("myDB", wrapper.MODE_PRIVATE, null);
    }

Upvotes: 0

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

You can't create a database without Context. This is one of the thing context exists for: it allows you to access shared preferences, database and so on.

Upvotes: 1

Related Questions