安卓:我可以用一个SQLiteOpenHelper类多个数据库文件? [英] Android: can I use one SQLiteOpenHelper class for multiple database files?

查看:148
本文介绍了安卓:我可以用一个SQLiteOpenHelper类多个数据库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用两个数据库(单独的文件)。为了处理这些数据库我已经创建了两个辅助类延伸SQLiteOpenHelper,每个数据库中。

My app uses two databases (separate files). To handle these databases I have created two Helper classes which extend SQLiteOpenHelper, one for each database.

我现在要添加第三个数据库,不知道我是否需要创建另一个辅助类(如果我使用了第4和第5个数据库,我需要更多的辅助类),或者我可以使用相同的助手类为多个数据库?

I am now going to add a third database and wonder whether I need to create yet another Helper class (and if I used a 4th and a 5th database would I need even more Helper classes), or can I use the same Helper class for multiple databases?

这是我与尝试使用只是一个辅助类中看到的问题是,我不能看到如何通过个人数据库文件复制到助手的名字。在present数据库的名称是硬codeD为每个辅助类的静态字段,但如果我只有一个辅助类,我需要能够在通过不同的名字到创建单独的辅助对象时,构造函数;的问题是,SQLiteOpenHelper构造似乎被称为由机器人只用一个参数:所述上下文

The problem that I see with trying to use just one Helper class is that I can't see how to pass the name of the individual database files to the Helper. At present the name of the database is hard-coded as a Static field of each of the Helper classes, but if I had only one Helper class I would need to be able to pass the different names in to the Constructor when creating the separate Helper objects; the problem is that the SQLiteOpenHelper Constructor seems to be called by Android with just one parameter: the Context.

推荐答案

当然,你可以。这只是你的助手一流的设计问题。您可以DB的名称只是传递给你的助手类的构造函数(连同所需的上下文实例),而不是硬编码:

Of course, you can. It is just a matter of your Helper class design. You can just pass the name of DB to your Helper class constructor (along with required Context instance) instead of hardcoding:

public class DBOpenHelper extends SQLiteOpenHelper {

    public Helper(Context context, String dbName, int dbVersion) {
        super(context, dbName, null, dbVersion);
    }
...
}

这篇关于安卓:我可以用一个SQLiteOpenHelper类多个数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆