Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - push sqlite database file into phone

I need to push sqlite database file to phone app storage location. I tried this for my app package database.But not working.Is there any way?

I have tried using below command in device shell, but getting device not found message.

shell@android: adb push MY_DB_FILE /data/data/MY_PACKAGE_NAME/databases/
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Call the write() method shown below whenever you want your database file. It will create a backupname.db file in the root folder of your device.(you can change the name of the .db file in the backupDBPath string)

private void write() throws IOException {
    File sd = Environment.getExternalStorageDirectory();

    if (sd.canWrite()) {

        String currentDBPath = DatabaseHelper.DATABASE_NAME;
        String backupDBPath = "backupname.db";
        File currentDB = new File(getDBPath(), currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) {
            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());

            src.close();
            dst.close();

        }
    }
}

private String getDBPath() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
    } else {
        return getFilesDir().getPath() + getPackageName() + "/databases/";
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...