从字符串资源中选择SQLite数据库名称

时间:2014-10-14 07:25:59

标签: java android android-sqlite android-resources

我的应用中有不同语言的单独数据库。在创建SQLiteOpenHelper类时,我在构造函数中传递DB_Name,如下所示

private static final String DB_NAME = "DB_en.sqlite";

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

现在,当我想要更改语言时,我需要通过"DB_zh.sqlite"来获取另一种语言。我将数据库名称存储在/res/values中作为不同语言的字符串资源,因此根据语言环境,它将采用名称。但是,由于我传递private static final String DB_NAME = "DB_en.sqlite";,如何将字符串资源传递给构造函数?我希望这个值来自字符串资源。

以下是我完整的数据库代码

public class MyDatabase {

    protected static final String TAG = "My Database";
    protected DataBaseHelper mDbHelper;
    protected SQLiteDatabase mDb;

    protected static final String DATABASE_NAME = "DB_en.sqlite";
    protected static final int DATABASE_VERSION = 1;

    protected final Context mCtx;

    public class DataBaseHelper extends SQLiteOpenHelper {

    // The Android's default system path of your application database.
    private final String DB_PATH = Environment.getDataDirectory()
            + "/data/com.tss.in.android.materialcompatibility/databases/";

    private static final String DB_NAME = "DB_en.sqlite";

    private SQLiteDatabase myDataBase;
    private final Context myContext;

    /**
     * Constructor Takes and keeps a reference of the passed context in
     * order to access to the application assets and resources.
     * 
     * @param context
     */     
    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        this.myContext = context;
    }

    public DataBaseHelper(Context context, String dbName) {
        super(context, dbName, null, DATABASE_VERSION);
        this.myContext = context;
    }

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();

        if (dbExist) {
            // do nothing - database already exist
        } else {
            this.getWritableDatabase();

            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            } finally {
                this.close();
            }
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;

        try {
            //String NAME = myContext.getResources().getString(R.string.dbname);
            String myPath = DB_PATH + DB_NAME;
            File dbFile = new File(myPath);
            return dbFile.exists();
        } catch (SQLiteException e) {
            // database does't exist yet.
            e.printStackTrace();
        }

        if (checkDB != null) {
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {
        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public void openDataBase() throws SQLException {
        // Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
        this.getWritableDatabase();
    }
}

1 个答案:

答案 0 :(得分:2)

super()构造函数调用中,您可以将DB_NAME替换为context.getString(R.string.db_name)并完全删除DB_NAME

对于资产问题的数据库,请考虑使用sqlite-asset-helper而不是基于古老博客文章的现有破解代码。