我创建了一个应用程序,我在SQLite数据库中保存了很少的信息。 我们知道SQLite数据库驻留在设备的内部存储器中,并在应用程序安装时创建,并在删除相应的应用程序时删除。
我正在发布该应用程序的新版本,所以当我删除旧版本以安装新版本时,我丢失了我在旧版本中保存的完整信息。
我的查询是可以以某种方式从其SQLite数据库中检索旧版本的数据,以便可以在较新版本中使用和维护。
请建议我,以免我的用户丢失旧数据。
答案 0 :(得分:3)
如果用户更新了应用程序,那么数据库将保持不变。卸载旧版本并重新安装新版本将导致数据库被删除。
答案 1 :(得分:1)
看到我的评论,我真的不认为你需要担心它。
但是,如果要备份和恢复数据库,以下方法会将其发送到SD卡,然后再读回(覆盖以前在那里的内容):
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"yourDB.bak");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data,
"data/your.package.name/databases/yourDB.sqlite");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}
public void restore() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File inputFile = new File(sdcard,
"yourDB.bak");
File data = Environment.getDataDirectory();
File outputFile = new File(data,
"data/your.package.name/databases/yourDB.sqlite");
if (!outputFile.exists())
outputFile.createNewFile();
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}