Android 数据库 openOrCreate() 错误
我有两个类,两者都不是活动,一个是执行数据库操作,一个是作为中介类转发值。 DataBase 类从第三个 Connectivity 类调用 openOrCreateDataBase 方法。
contextWrapper.openOrCreateDatabase(sqlDBName, MODE_PRIVATE, null);
但在这里,由于此类不是活动,我无法传递 ContextWrapper 的参数。有没有其他方法可以打开数据库。我已经尝试过,
sqLiteDatabase = SQLiteDatabase.openDatabase(path, factory, flags);
sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(file, factory);
但这些对我不起作用。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有上下文就无法创建数据库。这是上下文存在的目的之一:它允许您访问共享首选项、数据库等。
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.
是的,有办法。在 db 包装器类的构造函数中,您可以添加 ContextWrapper 作为参数并调用它,如下所示:
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: