使用append模式运行过程完成但是数据无法显示,只使用append模式
备份方法
public void backup(String outFileName, boolean appendMode) {
try {
File dbFile = new File(mContext.getDatabasePath(DATABASE_NAME).toString());
FileInputStream fis = new FileInputStream(dbFile);
// Open the output stream to the backup file
OutputStream output;
if (appendMode) {
output = new FileOutputStream(outFileName, true); // Append mode
//output.seek(output.length()); // Move cursor to end of file for append mode
} else {
output = new FileOutputStream(outFileName, false); // Overwrite mode
}
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
Toast.makeText(mContext, "Backup Completed", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(mContext, "Unable to backup database. Retry", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
导入方式
public void importDB(String inFileName, boolean appendMode) {
final String outFileName = mContext.getDatabasePath(DATABASE_NAME).toString();
try {
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
// Open the output stream to the existing database file
OutputStream output;
if (appendMode) {
output = new FileOutputStream(outFileName, true); // Append mode
} else {
// Open the output stream with the read-write flag and create if necessary
output = new FileOutputStream(outFileName, false); // Overwrite mode
}
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
Toast.makeText(mContext, "Import Completed", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(mContext, "Unable to import database. Retry", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
PerformBackup 方法
public void performBackup(final DBHelper db, final String outFileName, boolean appendMode) {
Permissions.verifyStoragePermissions(activity);
if (outFileName == null || outFileName.isEmpty()) {
Toast.makeText(activity, "Invalid backup file name. Please enter a valid file name.", Toast.LENGTH_SHORT).show();
return;
}
File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "/DewtreatDB");
boolean success = true;
if (!folder.exists())
success = folder.mkdirs();
if (success) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Backup Name");
builder.setPositiveButton("Save", (dialog, which) -> {
String m_Text = "dewtreat";
String out = outFileName + m_Text + ".db";
db.backup(out, appendMode); // Call backup() with appendMode
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
} else
Toast.makeText(activity, "Unable to create directory. Retry", Toast.LENGTH_SHORT).show();
}
PerformRestore 方法
public void performRestore(final DBHelper db, final boolean appendMode) {
Permissions.verifyStoragePermissions(activity);
File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "/DewtreatDB");
if (folder.exists()) {
final File[] files = folder.listFiles();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(activity, android.R.layout.select_dialog_item);
if (files != null && files.length > 0) {
for (File file : files) {
arrayAdapter.add(file.getName());
}
}
AlertDialog.Builder builderSingle = new AlertDialog.Builder(activity);
builderSingle.setTitle("Restore:");
builderSingle.setNegativeButton(
"cancel",
(dialog, which) -> dialog.dismiss());
builderSingle.setAdapter(
arrayAdapter,
(dialog, which) -> {
SQLiteDatabase database = db.getWritableDatabase();
database.beginTransaction();
try {
db.importDB(files[which].getPath(), appendMode);
database.setTransactionSuccessful();
} catch (Exception e) {
Toast.makeText(activity, "Unable to restore. Retry", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} finally {
// End the transaction
database.endTransaction();
}
});
builderSingle.show();
} else {
Toast.makeText(activity, "Backup folder not present.\nDo a backup before a restore!", Toast.LENGTH_SHORT).show();
}
}
我正在尝试将数据存储到本地数据库并从本地数据库恢复数据。当应用程序被卸载并重新安装以单击恢复时,必须显示现有存储,同时在导入现有数据并且已经有新数据时它也会合并并显示所有数据。