gpt4 book ai didi

Android - 数据库文件丢失时恢复数据库清除数据库

转载 作者:行者123 更新时间:2023-11-30 04:13:39 25 4
gpt4 key购买 nike

我找到了 tutorial将数据库复制到文件夹作为备份,并将其复制回应用程序的数据库文件夹作为还原。除了一件事,几乎一切正常:当我从我的文件夹中删除备份的 .db 文件并单击我的应用程序中的恢复时,它会查找该文件,找不到它,然后它会覆盖整个数据库。就像我清空了整个数据库。

备份:

try {

InputStream myInput;
OutputStream myOutput;

myInput = new FileInputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");//this is
// the path for all apps
//insert your package instead packagename,ex:com.mybusiness.myapp


// Set the output folder on the SDcard
File directory = new File("/sdcard/MyApp/");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
// Set the output file stream up:

myOutput = new FileOutputStream(directory.getPath()+
"/myapp.db");

// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams

myOutput.flush();

myOutput.close();

myInput.close();
Toast.makeText(Settings.this, "Backup done successfully!", Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {
Toast.makeText(Settings.this, "Backup unsuccessful! File cannot be created! Directory does not exist?", Toast.LENGTH_LONG).show();


// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(Settings.this, "Backup unsuccessful!", Toast.LENGTH_LONG).show();


// TODO Auto-generated catch block
e.printStackTrace();
}

恢复:

OutputStream myOutput;

try {

myOutput = new FileOutputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");


// Set the folder on the SDcard
File directory = new File("/sdcard/MyApp/");
// Set the input file stream up:

InputStream myInputs = new FileInputStream(directory.getPath()+ "/myapp.db");


// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInputs.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}


// Close and clear the streams
myOutput.flush();

myOutput.close();

myInputs.close();
Toast.makeText(Settings.this, "Restore done successfully!", Toast.LENGTH_SHORT).show();

} catch (FileNotFoundException e) {
Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory does not exist?", Toast.LENGTH_LONG).show();


// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { Toast.makeText(Settings.this, "Restore unsuccessful!",
Toast.LENGTH_SHORT).show();


// TODO Auto-generated catch block
e.printStackTrace();
}

编辑:我添加了 Jason 的答案,它仍然会清除数据库,即使我将 false 设置为 directoryB 并且 Restore 不成功!文件未找到!目录/文件不存在? 消息 ???!?!

try {
OutputStream myOutput;
myOutput = new FileOutputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");

// Set the folder on the SDcard
File directory = new File("/sdcard/MyApp/");
boolean directoryB = new File("/sdcard/MyApp/", "/myapp.db").exists();
Toast.makeText(Settings.this, "directoryB: " + directoryB, Toast.LENGTH_SHORT).show();

if (directoryB == true)
{
Toast.makeText(Settings.this, "File exists!", Toast.LENGTH_SHORT).show();

// Set the input file stream up:

InputStream myInputs = new FileInputStream(directory.getPath()+ "/myapp.db");


// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInputs.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}


// Close and clear the streams
myOutput.flush();

myOutput.close();

myInputs.close();
Toast.makeText(Settings.this, "Restore done successfully!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory/file does not exist?", Toast.LENGTH_SHORT).show();
}

} catch (FileNotFoundException e) {
//Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory does not exist?", Toast.LENGTH_LONG).show();


// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { Toast.makeText(Settings.this, "Restore unsuccessful!",
Toast.LENGTH_SHORT).show();


// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

在继续恢复操作之前先检查文件是否存在,如下所示:

new File(directory, "/myapp.db").exists()

FileInputStream 将在文件不存在时自动创建文件。

关于Android - 数据库文件丢失时恢复数据库清除数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10419637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com