do not delete SQLITE-Android database

1

It occurred to me to create an application that allows recovering user data when reinstalling it on the device. All the apps that I have previously created keep the information in a database and the information is lost when I uninstall the application. Is there any sure way to keep the information in a database even when the app is desistale ?. Thank you very much

    
asked by Kevtho 13.01.2017 в 19:43
source

2 answers

1

What I can think of is to be able to make a backup in a specific place, and keep in mind at what moment you program your backup copy and when uninstalling and installing it verify if the file exists to overwrite it. Here is a link to the backup section. Hope this can help you link

    
answered by 13.01.2017 в 20:03
1

You can make a copy of the database in your external storage but in a different route than the one used by your application.

Example, assuming your application has a package name called "com.kevtho.app" , you can use a different package to define another route, for example:

if you generate this route:

String mypath = Environment.getExternalStorageDirectory().getPath() +
"/Android/data/com.kevtho.basededatos/misbd";

by deleting your application this route that would contain the database, would remain intact.

Depending on the amount of data, if they are considerable or if it is more sensitive information, you may consider consuming a Web Service.

What this article mentions: Sqlite Backup to the sdcard unit on Android , it works but it is important that you change the name of the package to a different one from your application so that when it is deleted the data is not deleted.

public void backupdDatabase(){
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        String packageName  = "com.yourapp.package";
        String sourceDBName = "mydb.db";
        String targetDBName = "mydb";
        if (sd.canWrite()) {
            Date now = new Date();
        String currentDBPath = "data/" + packageName + "/databases/" + sourceDBName;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm"); 
        String backupDBPath = targetDBName + dateFormat.format(now) + ".db";

        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        Log.i("backup","backupDB=" + backupDB.getAbsolutePath());
        Log.i("backup","sourceDB=" + currentDB.getAbsolutePath());

        FileChannel src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        dst.transferFrom(src, 0, src.size());
        src.close();
        dst.close();
        }
    } catch (Exception e) {
        Log.i("Backup", e.toString());
    }
}
    
answered by 14.01.2017 в 00:25