推SQLite数据库文件到手机 [英] push sqlite database file into phone

查看:263
本文介绍了推SQLite数据库文件到手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要把SQLite数据库文件到手机应用程序的存储位置。
我试过<一个href=\"http://stackoverflow.com/questions/20834241/how-to-use-adb-command-to-push-a-file-on-device-without-sd-card\">this我的应用程序包database.But不working.Is有什么办法?

我在设备外壳使用下面的命令尝试过,但得到的设备没有找到消息。

 壳@机器人:亚行推MY_DB_FILE /数据/数据​​/ MY_PACKAGE_NAME /数据库/


解决方案

打电话时,你希望你的数据库文件如下所示的write()方法。它会在您的设备的根文件夹中的文件backupname.db(您可以更改backupDBPath字符串.db文件的名称)

 私人无效的write()抛出IOException
    文件SD = Environment.getExternalStorageDirectory();    如果(sd.canWrite()){        字符串currentDBPath = DatabaseHelper.DATABASE_NAME;
        字符串backupDBPath =backupname.db;
        文件currentDB =新的文件(getDBPath(),currentDBPath);
        文件的某个backupdb =新的文件(SD,backupDBPath);        如果(currentDB.exists()){
            FileChannel SRC =新的FileInputStream(currentDB).getChannel();
            FileChannel DST =新的FileOutputStream(某个backupdb).getChannel();
            dst.transferFrom(源,0,src.size());            src.close();
            dst.close();        }
    }
}私人字符串getDBPath(){    如果(Build.VERSION.SDK_INT&GT; = Build.VERSION_ codeS.JELLY_BEAN_MR1){
        返回getFilesDir()getAbsolutePath()代替(文件,数据库)+文件分割符。;
    }其他{
        返回getFilesDir()的getPath()+ getPackageName()+/数据库/。
    }
}

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/

解决方案

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/";
    }
}

这篇关于推SQLite数据库文件到手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆