Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // all import
- public class DataBaseHelper extends SQLiteOpenHelper
- {
- // The Android's default system path of your application database.
- private static final String DB_DEFAULT_FILE = "split-";
- private static String DB_PATH = "/data/data/com.cfdict.android/databases/";
- private static String DB_NAME = "cfdict.db";
- private SQLiteDatabase db;
- private static String SPLIT_FILES_PATH = "/assets/splitDb/";
- private static final String TAG = "CFDICT";
- private final Context mContext;
- /**
- * 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, 1);
- this.mContext = context;
- }
- /**
- * Creates a empty database on the system and rewrites it with your own
- * database.
- * */
- public void createDataBase() throws IOException
- {
- boolean dbExist = checkDataBase();
- if (dbExist)
- {
- // do nothing - database already exist
- Log.i(TAG, "createDataBase(): Database already exists");
- } else
- {
- try
- {
- // By calling this method an empty database will be created into the
- // default system path of your application so we are gonna be able to
- // overwrite that database with our database.
- Log.i(TAG, "createDataBase(): Create an empty database");
- this.getReadableDatabase();
- } catch (SQLiteException sqliteE)
- {
- sqliteE.printStackTrace();
- Log.e(TAG, "Error database cannot be opened");
- throw new Error("Error database cannot be opened");
- }
- try
- {
- Log.i(TAG, "createDataBase(): Copy database…");
- copyDataBase();
- } catch (IOException ioE)
- {
- Log.e(TAG, "createDataBase(): FAIL to copy database…");
- throw new Error("Error copying database");
- }
- }
- }
- /**
- * Get the list of splitted files to import in the new Database
- *
- * @return list of files under splitDb/
- */
- private String[] getAssetDbFiles()
- {
- AssetManager mAM = mContext.getAssets();
- String splitFileArray[] = null;
- try
- {
- Log.i(TAG, "getAssetDbFiles(): Import multiple files…");
- splitFileArray = mAM.list(SPLIT_FILES_PATH);
- if (splitFileArray != null)
- {
- Log.i(TAG, "getAssetDbFiles(): Splitted files found!");
- } else
- {
- Log.v(TAG, "getAssetDbFiles(): NO Splitted files found!"
- + SPLIT_FILES_PATH);
- }
- } catch (IOException e)
- {
- throw new Error("Fail to list: " + SPLIT_FILES_PATH);
- }
- return splitFileArray;
- }
- /**
- * Check if the database already exist to avoid re-copying the file each time
- * you open the application.
- *
- * @return true if it exists, false if it doesn't
- */
- private boolean checkDataBase()
- {
- SQLiteDatabase checkDB = null;
- Log.i(TAG, "Opening Database…");
- try
- {
- String mPath = DB_PATH + DB_NAME;
- checkDB = SQLiteDatabase.openDatabase(mPath, null,
- SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
- Log.i(TAG, "checkDataBase(): Database is OPEN");
- } catch (SQLiteException e)
- {
- // database does't exist yet.
- Log.i(TAG, "checkDataBase(): Database DOESN'T exists");
- } finally
- // ALWAYS executed even if try fail
- {
- if (checkDB != null)
- {
- checkDB.close();
- }
- }
- return checkDB != null ? true : false;
- }
- /**
- * Copies your database from your local assets-folder to the just created
- * empty database in the system folder, from where it can be accessed and
- * handled. This is done by transfering bytestream.
- * */
- private void copyDataBase() throws IOException
- {
- String outFileName = DB_PATH + DB_NAME; // Path to the just created empty db
- OutputStream mOutput = null; // Output stream for the Db
- String dbSplitPartName = null; // chunk filename
- InputStream mInput = null; // stream to a chunk
- try
- {
- // Open the empty db as the output stream
- mOutput = new FileOutputStream(outFileName);
- // Get list of chunk files to copy into the Database
- List<String> splitFileList = new ArrayList<String>();
- String[] chunkArray = getAssetDbFiles();
- for (String chunk : chunkArray)
- {
- Log.v(TAG, "copyDataBase(): Importing chunk: " + chunk);
- try
- {
- // Open your local db as the input stream
- mInput = mContext.getAssets().open(chunk);
- // transfer bytes from the inputfile to the outputfile
- byte[] buffer = new byte[1024];
- int length;
- while ((length = mInput.read(buffer)) > 0)
- {
- mOutput.write(buffer, 0, length);
- }
- } catch (IOException e)
- {
- Log.e(TAG, "copyDataBase(): CAN'T OPEN chunk: " + chunk);
- e.printStackTrace();
- }
- }
- // // add all element from the Array to the List
- // Collections.addAll(splitFileList, chunkArray);
- //
- // for (Iterator<String> it = splitFileList.iterator(); it.hasNext();)
- // {
- // dbSplitPartName = it.next(); // get next item (starting with none)
- // Log.v(TAG,
- // "copyDataBase(): Importing chunk: " + dbSplitPartName.toString());
- //
- // // Open your local db as the input stream
- // mInput = mContext.getAssets().open(dbSplitPartName);
- //
- // // transfer bytes from the inputfile to the outputfile
- // byte[] buffer = new byte[1024];
- // int length;
- // while ((length = mInput.read(buffer)) > 0)
- // {
- // mOutput.write(buffer, 0, length);
- // }
- // }
- } catch (FileNotFoundException e)
- {
- Log.e(TAG, "copyDataBase(): CAN'T OPEN output stream/file: "
- + outFileName.toString());
- e.printStackTrace();
- } finally
- {
- if (mOutput != null)
- {// Close the streams
- mOutput.flush();
- mOutput.close();
- }
- if (mInput != null)
- {
- mInput.close();
- }
- }
- }
- public void openDataBase() throws SQLException
- {
- // Open the database
- String mPath = DB_PATH + DB_NAME;
- db = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.OPEN_READONLY);
- }
- @Override
- public synchronized void close()
- {
- if (db != null) db.close();
- super.close();
- }
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
- }
- // Add your public helper methods to access and get content from the database.
- // You could return cursors by doing "return mDataBase.query(....)" so it'd
- // be easy
- // to you to create adapters for your views.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement