出于备份目的,我正在try 实现一个简单的SQLite导出/导入.导出只是存储原始current.db文件的副本.对于导入,我只想删除旧的current.db文件,并将imported.db文件重命名为current.db.这个是可能的吗?当我try 此解决方案时,我得到以下错误:

06-30 13:33:38.831: ERROR/SQLiteOpenHelper(23570):
    android.database.sqlite.SQLiteDatabaseCorruptException: error code 11: database disk image is malformed

如果我在SQLite浏览器中查看原始数据库文件,它看起来没有问题.

推荐答案

我在我的一个应用程序的SQLiteOpenHelper中使用这段代码来导入数据库文件.

编辑:我将我的FileUtils.copyFile()方法粘贴到问题中.

SQLiteOpenHelper

public static String DB_FILEPATH = "/data/data/{package_name}/databases/database.db";

/**
 * Copies the database file at the specified location over the current
 * internal application database.
 * */
public boolean importDatabase(String dbPath) throws IOException {

    // Close the SQLiteOpenHelper so it will commit the created empty
    // database to internal storage.
    close();
    File newDb = new File(dbPath);
    File oldDb = new File(DB_FILEPATH);
    if (newDb.exists()) {
        FileUtils.copyFile(new FileInputStream(newDb), new FileOutputStream(oldDb));
        // Access the copied database so SQLiteHelper will cache it and mark
        // it as created.
        getWritableDatabase().close();
        return true;
    }
    return false;
}

FileUtils

public class FileUtils {
    /**
     * Creates the specified <code>toFile</code> as a byte for byte copy of the
     * <code>fromFile</code>. If <code>toFile</code> already exists, then it
     * will be replaced with a copy of <code>fromFile</code>. The name and path
     * of <code>toFile</code> will be that of <code>toFile</code>.<br/>
     * <br/>
     * <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
     * this function.</i>
     * 
     * @param fromFile
     *            - FileInputStream for the file to copy from.
     * @param toFile
     *            - FileInputStream for the file to copy to.
     */
    public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
        FileChannel fromChannel = null;
        FileChannel toChannel = null;
        try {
            fromChannel = fromFile.getChannel();
            toChannel = toFile.getChannel();
            fromChannel.transferTo(0, fromChannel.size(), toChannel);
        } finally {
            try {
                if (fromChannel != null) {
                    fromChannel.close();
                }
            } finally {
                if (toChannel != null) {
                    toChannel.close();
                }
            }
        }
    }
}

如果需要,不要忘记删除旧的数据库文件.

Database相关问答推荐

在MongoDB的树形 struct 中更新具有给定id的元素

使用mongoose 在嵌套对象中查找特定字段

有使用 H2 数据库的实际经验吗?

多列上的全文索引如何工作?

Cassandra 还是 MySQL/PostgreSQL?

如何更改 Heroku 中的列类型?

使用 Mongoose 删除索引的推荐方法是什么?

PostgreSQL 将列从整数转换为文本

PostgreSQL 是否对只读事务进行了一些性能优化

如何在 Windows 中安装 InfluxDB?

Select 最佳主键 + 编号系统

如何从 MySQL 行中修剪前导和尾随引号?

如何删除 SQLite 中具有多个 where 参数的行?

如何使用 MySQL Workbench 更改字段的值?

Django 数据库值的最佳推荐方式是什么?

您最喜欢在 django 中管理数据库迁移的解决方案是什么?

如何在 SSIS 中插入新记录之前清空我的目标表?

如何在没有验证提取的情况下将 MSSQLServer 数据库提取为 .dacpac?

使用 PHP/MySQL 导入 CSV 数据

查询以查找列的 nᵗʰ 最大值