Guest User

Untitled

a guest
Aug 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. Can't copy SQLite database from assets
  2. public class DatabaseAdapter {
  3. private static String DB_PATH = "/data/data/com.mypackage/databases/";
  4. private static String DB_NAME = "database.sqlite";
  5. private static String TABLE_NAME = "content_table";
  6.  
  7. private SQLiteDatabase database = null;
  8. private final Context context;
  9.  
  10. public DatabaseAdapter(Context context){
  11. this.context = context;
  12. }
  13.  
  14. private void openDatabase() throws SQLiteException{
  15. DatabaseHelper databaseHelper = new DatabaseHelper(context, DB_NAME);
  16. SQLiteDatabase db = null;
  17. if(!checkDatabase()){
  18. try{
  19. //Tried to create db before copying, so file should exist
  20. db = databaseHelper.getReadableDatabase();
  21. db.close();
  22.  
  23. copyDatabase();
  24.  
  25. }catch(IOException exception){
  26. Log.d("DatabaseAdapter", "Error copying DB: "+exception);
  27. }
  28. }
  29.  
  30. database = SQLiteDatabase.openDatabase(DB_PATH+DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
  31. }
  32.  
  33. private void closeDatabase(){
  34. database.close();
  35. }
  36.  
  37. public ArrayList<String> queryCategories(){
  38. try{
  39. openDatabase();
  40. }catch(SQLiteException exc){
  41. exc.printStackTrace();
  42. }
  43. //.............................
  44. return result;
  45. }
  46.  
  47. private boolean checkDatabase(){
  48. File dbFile = new File(DB_PATH + DB_NAME);
  49. return dbFile.exists();
  50. }
  51.  
  52. private void copyDatabase() throws IOException{
  53. InputStream inputStream = context.getAssets().open(DB_NAME);
  54.  
  55. String outFileName = DB_PATH + DB_NAME;
  56.  
  57. OutputStream outputStream = new FileOutputStream(outFileName);
  58.  
  59. byte[] buffer = new byte[1024];
  60. int length;
  61. while ((length = inputStream.read(buffer))>0){
  62. outputStream.write(buffer, 0, length);
  63. }
  64.  
  65. outputStream.flush();
  66. outputStream.close();
  67. inputStream.close();
  68. }
  69.  
  70. }
  71.  
  72. ublic class DatabaseHelper extends SQLiteOpenHelper {
  73.  
  74. public DatabaseHelper(Context context, String name){
  75. super(context, name, null, 1);
  76.  
  77. }
  78.  
  79.  
  80. @Override
  81. public void onCreate(SQLiteDatabase arg0) {
  82. // TODO Auto-generated method stub
  83.  
  84. }
  85.  
  86. @Override
  87. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  88. // TODO Auto-generated method stub
  89.  
  90. }
  91.  
  92. }
  93.  
  94. public class DBHelper extends SQLiteOpenHelper{
  95.  
  96. private final static String DB_PATH = "/data/data/[YOUR PACKAGE HERE]/databases/";
  97.  
  98. String dbName;
  99. Context context;
  100.  
  101. File dbFile;
  102.  
  103. public DBHelper(Context context, String dbName, CursorFactory factory,
  104. int version) {
  105. super(context, dbName, factory, version);
  106. this.context = context;
  107. this.dbName = dbName;
  108. dbFile= new File(DB_PATH + dbName);
  109. }
  110.  
  111. @Override
  112. public synchronized SQLiteDatabase getWritableDatabase() {
  113.  
  114. if(!dbFile.exists()){
  115. SQLiteDatabase db = super.getWritableDatabase();
  116. copyDataBase(db.getPath());
  117. }
  118. return super.getWritableDatabase();
  119. }
  120.  
  121. @Override
  122. public synchronized SQLiteDatabase getReadableDatabase() {
  123. if(!dbFile.exists()){
  124. SQLiteDatabase db = super.getReadableDatabase();
  125. copyDataBase(db.getPath());
  126. }
  127. return super.getReadableDatabase();
  128. }
  129.  
  130. @Override
  131. public void onCreate(SQLiteDatabase db) {}
  132.  
  133. @Override
  134. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
  135.  
  136. private void copyDataBase(String dbPath){
  137. try{
  138. InputStream assestDB = context.getAssets().open("databases/"+dbName);
  139.  
  140. OutputStream appDB = new FileOutputStream(dbPath,false);
  141.  
  142. byte[] buffer = new byte[1024];
  143. int length;
  144. while ((length = assestDB.read(buffer)) > 0) {
  145. appDB.write(buffer, 0, length);
  146. }
  147.  
  148. appDB.flush();
  149. appDB.close();
  150. assestDB.close();
  151. }catch(IOException e){
  152. e.printStackTrace();
  153. }
  154.  
  155. }
  156.  
  157. }
Add Comment
Please, Sign In to add comment