Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Can't copy SQLite database from assets
- public class DatabaseAdapter {
- private static String DB_PATH = "/data/data/com.mypackage/databases/";
- private static String DB_NAME = "database.sqlite";
- private static String TABLE_NAME = "content_table";
- private SQLiteDatabase database = null;
- private final Context context;
- public DatabaseAdapter(Context context){
- this.context = context;
- }
- private void openDatabase() throws SQLiteException{
- DatabaseHelper databaseHelper = new DatabaseHelper(context, DB_NAME);
- SQLiteDatabase db = null;
- if(!checkDatabase()){
- try{
- //Tried to create db before copying, so file should exist
- db = databaseHelper.getReadableDatabase();
- db.close();
- copyDatabase();
- }catch(IOException exception){
- Log.d("DatabaseAdapter", "Error copying DB: "+exception);
- }
- }
- database = SQLiteDatabase.openDatabase(DB_PATH+DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
- }
- private void closeDatabase(){
- database.close();
- }
- public ArrayList<String> queryCategories(){
- try{
- openDatabase();
- }catch(SQLiteException exc){
- exc.printStackTrace();
- }
- //.............................
- return result;
- }
- private boolean checkDatabase(){
- File dbFile = new File(DB_PATH + DB_NAME);
- return dbFile.exists();
- }
- private void copyDatabase() throws IOException{
- InputStream inputStream = context.getAssets().open(DB_NAME);
- String outFileName = DB_PATH + DB_NAME;
- OutputStream outputStream = new FileOutputStream(outFileName);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = inputStream.read(buffer))>0){
- outputStream.write(buffer, 0, length);
- }
- outputStream.flush();
- outputStream.close();
- inputStream.close();
- }
- }
- ublic class DatabaseHelper extends SQLiteOpenHelper {
- public DatabaseHelper(Context context, String name){
- super(context, name, null, 1);
- }
- @Override
- public void onCreate(SQLiteDatabase arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- }
- }
- public class DBHelper extends SQLiteOpenHelper{
- private final static String DB_PATH = "/data/data/[YOUR PACKAGE HERE]/databases/";
- String dbName;
- Context context;
- File dbFile;
- public DBHelper(Context context, String dbName, CursorFactory factory,
- int version) {
- super(context, dbName, factory, version);
- this.context = context;
- this.dbName = dbName;
- dbFile= new File(DB_PATH + dbName);
- }
- @Override
- public synchronized SQLiteDatabase getWritableDatabase() {
- if(!dbFile.exists()){
- SQLiteDatabase db = super.getWritableDatabase();
- copyDataBase(db.getPath());
- }
- return super.getWritableDatabase();
- }
- @Override
- public synchronized SQLiteDatabase getReadableDatabase() {
- if(!dbFile.exists()){
- SQLiteDatabase db = super.getReadableDatabase();
- copyDataBase(db.getPath());
- }
- return super.getReadableDatabase();
- }
- @Override
- public void onCreate(SQLiteDatabase db) {}
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
- private void copyDataBase(String dbPath){
- try{
- InputStream assestDB = context.getAssets().open("databases/"+dbName);
- OutputStream appDB = new FileOutputStream(dbPath,false);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = assestDB.read(buffer)) > 0) {
- appDB.write(buffer, 0, length);
- }
- appDB.flush();
- appDB.close();
- assestDB.close();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment