Reputation: 2519
I made an application in which I save few information in my SQLite database. As we know that SQLite database resides in the internal memory of the device and its created while app installation and deleted when we remove the respective application.
I am releasing a new version of the application, so when I delete my older version to install the newer version I lost the complete information which I had saved in the older version.
My query is can i retrieve the data from the older version from its SQLite database somehow so that, it can be used and maintained in the newer version.
Please suggest me so that my users must not lost their old data.
Upvotes: 1
Views: 1214
Reputation: 35661
If the user updates the application then the database will remain intact. Uninstalling the old version and re-installing the new version will cause the database to be deleted.
Upvotes: 3
Reputation: 16393
See my comment, I really don't think you need to worry about it.
However, if you want to back up and restore your database, the following methods will send it to the SD Card and then read it back in (overwriting what was previously there in each case):
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");
}
}
Upvotes: 1