Can't copy SQLite database from assets?

Up vote 0 down vote favorite share g+ share fb share tw.

I try to copy SQLite database from assets directory to access it later. But I fail to do it! 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 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 byte1024; int length; while ((length = inputStream. Read(buffer))>0){ outputStream.

Write(buffer, 0, length); } outputStream.flush(); outputStream.close(); inputStream.close(); } } DatabaseHelper is simple: 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 } } Tried everything! I tried playing with extention! But I still get an error: Error copying DB: java.io.

FileNotFoundException: /data/data/com. Mypackage/databases/database. Sqlite (No such file or directory) I checked on emulator, my file is there, so I should be able to write to it!

Please, any help! It's driving me nuts! UPD I tried to place it on SD card and it worked.

But still can't get why I can't write it to app data folder. Java android sqlite link|improve this question edited Nov 8 '11 at 14:57 asked Nov 8 '11 at 14:50Mighter1,7851526 81% accept rate.

I use this Helper and works fine: 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 byte1024; int length; while ((length = assestDB. Read(buffer)) > 0) { appDB.

Write(buffer, 0, length); } appDB.flush(); appDB.close(); assestDB.close(); }catch(IOException e){ e.printStackTrace(); } } } Take into account that the file extension of a database is . Db and that my databases are into assets/databases.

Thanks! It worked, I'll try now to figure out what was wrong – Mighter Nov 8 '11 at 15:22.

Db = databaseHelper. GetWritableDatabase(); //instead of getReadableDatabase try write.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions