android将数据库从内部存储复制到外部存储返回空数据库

标签 android database

我正在尝试将数据库从内部存储复制到外部存储,但它没有复制数据库。它只是创建一个新的空 sqlite 文件。我实际上缺少什么想法吗?

这是我正在使用的:

    public static void copyFile(String currentDBPath, String backupDBPath){
    try {
        File sd = Environment.getExternalStorageDirectory();
        //File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File currentDB = new File(currentDBPath);
            File backupDB = new File(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();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

并像这样使用它:

File database = new File("/sdcard/Stam/Data/");
        database.mkdirs();
        RPCCommunicator.copyFile("/data/data/" + this.getPackageName()+ "/databases/stam_sys_tpl.sqlite","/sdcard/Stam/Data/system.sqlite");

因此,使用它,它会创建 system.sqlite,但它是空的,没有任何表。我想做的事情是将数据库从当前目录移动到新目录,而不丢失任何数据。

知道为什么它不能正常工作吗?

最佳答案

使用Context.getDataBasePath()获取您的数据库文件!

关于android将数据库从内部存储复制到外部存储返回空数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9078915/

相关文章:

mysql - 多个服务器和数据库分区上的 sphinx

mysql - 显示从属主机;有一个空的主机输出。如何获取从属主机?

android - 在Android Listview项目中显示项目及其信息

android - 如何在android中为圆角设置颜色

java - LinearLayout onShow 它将 ScrollView 向下移动

mysql - mysql中表的使用

android - 自定义 View - 将 View 移动到嵌套布局中

android - 每次 Activity 都开始创建该类的新对象吗?

php - 使用 phpcassa 显示所有键

database - 表关系逻辑