如何在android中使用多种数据库,如多种语言

如何在android中使用多种数据库,如多种语言,android,sqlite,Android,Sqlite,我有两个数据库来支持我的应用程序的两种语言 当我从设置更改语言时,我想更改数据库 有可能吗??如何执行此操作?当您提供一个带有数据库名称的字符串资源时,应该可以非常轻松地执行此操作: 在/res/values/strings.xml中放一行如下: <string name="db_name">database</string> 在DBHelper类中,根据语言设置使用当前活动的字符串文件的数据库名称: public class DBHelper extends SQLi

我有两个数据库来支持我的应用程序的两种语言

当我从设置更改语言时,我想更改数据库


有可能吗??如何执行此操作?

当您提供一个带有数据库名称的字符串资源时,应该可以非常轻松地执行此操作:

/res/values/strings.xml
中放一行如下:

<string name="db_name">database</string>
在DBHelper类中,根据语言设置使用当前活动的字符串文件的数据库名称:

public class DBHelper extends SQLiteOpenHelper {

    private final static int DB_VERSION = 1;
    private static DBHelper sInstance;

    /**
     * Provides access to DBHelper singleton.
     * @param context
     * @return
     */
    public static DBHelper getInstance(Context context) {
        // Use the application context, which will ensure that you
        // don't accidentally leak an Activity's context.
        // See this article for more information: http://bit.ly/6LRzfx
        if (sInstance == null) {
            sInstance = new DBHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    /**
     * Constructor should be private to prevent direct instantiation.
     * make call to static factory method "getInstance()" instead.
     */
    private DBHelper(Context context) {
        // here comes the magic:
        String dbName = context.getString(R.string.db_name);
        super(context, db_name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
         // ...
    }

    // ...
}

当您提供一个带有数据库名称的字符串资源时,应该很容易做到:

/res/values/strings.xml
中放一行如下:

<string name="db_name">database</string>
在DBHelper类中,根据语言设置使用当前活动的字符串文件的数据库名称:

public class DBHelper extends SQLiteOpenHelper {

    private final static int DB_VERSION = 1;
    private static DBHelper sInstance;

    /**
     * Provides access to DBHelper singleton.
     * @param context
     * @return
     */
    public static DBHelper getInstance(Context context) {
        // Use the application context, which will ensure that you
        // don't accidentally leak an Activity's context.
        // See this article for more information: http://bit.ly/6LRzfx
        if (sInstance == null) {
            sInstance = new DBHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    /**
     * Constructor should be private to prevent direct instantiation.
     * make call to static factory method "getInstance()" instead.
     */
    private DBHelper(Context context) {
        // here comes the magic:
        String dbName = context.getString(R.string.db_name);
        super(context, db_name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
         // ...
    }

    // ...
}

使用Ridcully解决方案,只需添加数据库名称部分, 您只需要添加扩展的类

此示例来自您提到的库:

public class MyDatabase extends SQLiteAssetHelper {


    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context) {
        // here comes the magic:
        String dbName = context.getString(R.string.db_name);
        super(context, dbName , null, DATABASE_VERSION);
    }
}
其他数据库类可以是:

public class MyDatabase2 extends SQLiteAssetHelper {


    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context) {
        // here comes the magic:
        String dbName = context.getString(R.string.db_name_2);
        super(context, dbName , null, DATABASE_VERSION);
    }
}
注意:要使用SQLiteAssetHelper,您需要自己创建数据库,并放入assets/databases文件夹


您可以使用此应用程序创建sqlite数据库

使用Ridcully解决方案,您只需要添加数据库名称部分, 您只需要添加扩展的类

此示例来自您提到的库:

public class MyDatabase extends SQLiteAssetHelper {


    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context) {
        // here comes the magic:
        String dbName = context.getString(R.string.db_name);
        super(context, dbName , null, DATABASE_VERSION);
    }
}
其他数据库类可以是:

public class MyDatabase2 extends SQLiteAssetHelper {


    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context) {
        // here comes the magic:
        String dbName = context.getString(R.string.db_name_2);
        super(context, dbName , null, DATABASE_VERSION);
    }
}
注意:要使用SQLiteAssetHelper,您需要自己创建数据库,并放入assets/databases文件夹


您可以使用此应用程序创建sqlite数据库

您还可以创建一个名为
translations
的不同表,在其中存储所有名称和内容。例如,如果您有一个带有
productid
的表
products
,一个带有列
productid
的表
product\u translations
languagekey
和翻译。(这只是我过去的做法)你的意思是使用一个数据库???是的,我只使用了一个数据库,只有两个表。不是。我不想要oane数据库。因为我的数据库非常复杂。你也可以创建一个不同的表,名为
translations
,用于存储所有名称和内容。例如,如果您有一个带有
productid
的表
products
,一个带有列
productid
的表
product\u translations
languagekey
和翻译。(这只是我过去的做法)你的意思是使用一个数据库???是的,我只使用了一个数据库,只有两个表。不是。我不想要oane数据库。因为我的数据库很复杂,我会试试的。但是我有一个小问题。我将预加载的数据库与此链接一起使用:。你能告诉我怎么做吗?为什么不问问@MBH,你接受了谁的答案(这是我的一份副本)?我会试试。但是我有一个小问题。我将预加载的数据库与此链接一起使用:。你能告诉我怎么做吗?为什么不问问@MBH,你接受了谁的答案(这是我的一份副本)??