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

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

问题描述

我的应用程式使用两个资料库(不同的档案)。为了处理这些数据库,我创建了两个Helper类,扩展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.

我现在要添加第三个数据库,还有另一个Helper类(如果我使用第4和第5个数据库,我需要更多的Helper类),或者我可以为多个数据库使用相同的Helper类吗?

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?

我试图使用只有一个Helper类的问题是,我看不到如何将个别数据库文件的名称传递给助手。目前数据库的名称被硬编码为每个Helper类的静态字段,但是如果我只有一个Helper类,我需要能够在创建单独的Helper时将不同的名称传递给构造函数对象;问题是,SQLiteOpenHelper构造函数似乎只有一个参数调用Android:上下文。

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.

推荐答案

能够。这只是你的Helper类设计的问题。您可以将DB的名称传递给您的Helper类构造函数(以及必需的 Context 实例),而不是硬编码:

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 DBOpenHelper(Context context, String dbName, int dbVersion) {
        super(context, dbName, null, dbVersion);
    }
...
}

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

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