热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java导出sqlite_java–在Android上简单导出和导入SQLite数据库

我在SQLiteOpenHelper中使用这个代码在我的一个应用程序中导入数据库文件。编辑:我粘贴我的FileUtils.copyFile()方法到问题。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 toFile as a byte for byte copy of the

* fromFile. If toFile already exists, then it

* will be replaced with a copy of fromFile. The name and path

* of toFile will be that of toFile.

*

* Note: fromFile and toFile will be closed by

* this function.

*

* @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();

}

}

}

}

}

如有必要,请不要忘记删除旧的数据库文件。



推荐阅读
author-avatar
手机用户2502902093
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有