开发者

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 o开发者_开发技巧f 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


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.


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜