Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - Can't copy SQLite database from assets

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

}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...