合并Android中的两个数据库

时间:2014-06-26 10:01:55

标签: java android database import restore

在我的应用程序中,我实现了备份和恢复数据库到SD卡的选项。恢复数据库时,我必须将备份数据中包含当前保存的数据。如何做到这一点?

这就是我恢复数据库的方式。

还原:

 try {
            File sd = Environment.getExternalStorageDirectory();
            File data  = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String  currentDBPath= "//data//" + "PackageName"
                        + "//databases//" + "DatabaseName";
                String backupDBPath  = "/BackupFolder/DatabaseName";
                File  backupDB= new File(data, currentDBPath);
                File currentDB  = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dest = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getBaseContext(), backupDB.toString(),
                        Toast.LENGTH_LONG).show();

            }
        } catch (Exception e) {

            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();

        }

1 个答案:

答案 0 :(得分:0)

尝试以这种方式将支持附加到当前数据库:

sqlite> ATTACH DATABASE 'testDB1.db' as 'DB1';
sqlite> ATTACH DATABASE 'testDB2.db' as 'DB2';
sqlite> ATTACH DATABASE 'testDB3.db' as 'DB3';

SQLite - ATTACH Database