android - 在 Android 中备份 SQLite 数据库
问题描述
我需要在移动设备的内部存储或 SD 卡中对 SQLite 数据库进行离线备份。但我一无所知,这怎么可能。
我在这里关注了很多线程,但他们建议将数据库复制到另一个位置,这不适合我,也不适合Google Drive
.
任何指导都是宝贵的。
解决方案
我使用了这种方法。
public string DATABASE_NAME = "YOUR DATABASE NAME HERE";
public void exportDB(){
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
Log.d("TAG", "DatabaseHandler: can write in sd");
//Replace with YOUR_PACKAGE_NAME and YOUR_DB_NAME
String currentDBPath = "filepath here"+DATABASE_NAME;
//Replace with YOUR_FOLDER_PATH and TARGET_DB_NAME in the SD card
String copieDBPath = DATABASE_NAME;
File currentDB = new File(data, currentDBPath);
File copieDB = new File(sd, copieDBPath);
if (currentDB.exists()) {
Log.d("TAG", "DatabaseHandler: DB exist");
@SuppressWarnings("resource")
FileChannel src = new FileInputStream(currentDB).getChannel();
@SuppressWarnings("resource")
FileChannel dst = new FileOutputStream(copieDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
推荐阅读
- powershell - Powershell - Office365 Exchange 入站和出站邮件
- python - 从熊猫系列中过滤熊猫df多列
- python - 将json文件中的数据提取到csv中
- c# - 重新加载后如何使我的当前 EXP 整数不重置为 0
- python - 从下拉列表组合中提取所有链接
- javascript - 多个 HTML 输入范围类型的标签总是加起来一个特定的值?
- javascript - 获取最高建筑物名称和高度
- r - 从长字符串中提取值并根据 r 中的括号数创建新列
- javascript - 焦点模糊上的 MS Edge 轮廓伪影?
- angularjs - 在输入日期字段中输入合法日期之前,如何禁用提交按钮?