数据库的本地化

时间:2014-10-13 08:37:41

标签: android sqlite locale

在我的应用程序中,我为两种不同的语言创建了两个不同的数据库,数据库都位于assets文件夹中,基于它应该占用该特定数据库的语言环境。不知道如何实现它,我虽然将数据库名称放在不同语言的字符串文件夹中,但如何将该名称作为DB_NAME传递并不确定。想知道如何在资产文件夹中有两个不同的数据库时本地化数据库。下面是我的代码

public class MatDatabase {

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

    protected static final String DATABASE_NAME = "ability.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.in.android.ability/databases/";

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

        private SQLiteDatabase myDataBase;

        private final Context myContext;

        public DataBaseHelper(Context context) {

            super(context, DB_NAME, null, DATABASE_VERSION);

            this.myContext = context;
            //myContext.getResources().getString(R.string.dbname);
        }


        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 + 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();

        }}

0 个答案:

没有答案