Can't copy SQLite database from assets

后端 未结 4 1667
情书的邮戳
情书的邮戳 2020-12-04 00:05

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         


        
相关标签:
4条回答
  • 2020-12-04 00:41

    Please check the databases folder before your OutputStream.

    like this,

    File databaseFile = new File(context.getFilesDir().getAbsolutePath()
                .replace("files", "databases"));
    
    // check if databases folder exists, if not create it.
    if (!databaseFile.exists()){
        databaseFile.mkdir();
    }
    
    0 讨论(0)
  • 2020-12-04 00:42

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

    Take into account that the file extension of a database is .db and that my databases are into assets/databases/

    0 讨论(0)
  • 2020-12-04 00:53
    public static void copyDatabase(final Context ctx, String dbName) {
        if (ctx != null) {
            File f = ctx.getDatabasePath(dbName);
            if (!f.exists()) {
    
                // check databases exists
                if (!f.getParentFile().exists())
                    f.getParentFile().mkdir();
    
                try {
                    InputStream in = ctx.getAssets().open(dbName);
                    OutputStream out = new FileOutputStream(f.getAbsolutePath());
    
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = in.read(buffer)) > 0) {
                        out.write(buffer, 0, length);
                    }
                    in.close();
                    out.close();
                    Logger.i("Database copy successed! " + f.getPath());
                } catch (Exception ex) {
                    Logger.e(ex);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 00:57

    I have the same problem and I have fixed it with another approach. At the beginning, I declared the database path as everyone did:

    dbPath="data/data/<my package name>/databases/data.db"
    

    This is an exactly path, no mistake. But It' always fail when I try to open the OutPutFileStream to copy database. I don't know why. And then, I change the way to open the database as below:

    dbPath = context.getDatabasePath(dbName);
    OutputStream myOutput = new FileOutputStream(dbPath.getAbsolutePath());
    

    The problem has ben solved. So surprise.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题