Android save database after uninstalling application

I am working on a sqlite database where I can find a problem on a Samsung device. When I uninstall the Android application, OS supports the database, which causes conflicts in the database version - I used to use version 2, and now I use version 1 as a parameter in the constructor sqllitehelper. By convention, when you uninstall an application, Android deletes all databases, shared preferences, and cache files.

I used the default database repository location in the application folder.

This is the error I get:

Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
+3
source share
4 answers

SQlite - , : ( ) (/data/data/$PACKAGENAME/databases). .

SD-, . , .

0

SDCard:

public void writeToSDCard() throws IOException {
    File f=new File("/data/data/com.YourPackage/databases/YourDB");
    FileInputStream fis=null;
    FileOutputStream fos=null;
    try{
        fis=new FileInputStream(f);
        fos=new FileOutputStream("/mnt/sdcard/backup.db");
        while(true){
            int i=fis.read();
            if(i!=-1){
                fos.write(i);
            }
            else{
                break;
            }
        }
        fos.flush();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try{
            fos.close();
            fis.close();
        }
        catch(IOException ioe){
            System.out.println(ioe);
        }
    }
} 
0

Sqlite SD-Card. , : -

1) permission Manifest.XML :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2) Java:

public boolean packupDatabaseFile(){
    try {
        File sdCardDir = Environment.getExternalStorageDirectory();
        File dataDir = Environment.getDataDirectory();

        if (sdCard.canWrite()) {
            String dbPath = "//data//{your package name}//databases//{your database name}";
            String backupDBPath = "{database name}";
            File dbFile = new File(dataDir , currentDBPath);
            File backupDbFile = new File(sdCardDir , backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(dbFile ).getChannel();
                FileChannel dst = new FileOutputStream(backupDbFile ).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    return true;//Success
    } catch (Exception e) {
    return false;//Failed to backup
    }
}

Using this Question

0
source

How do you use SQLiteOpenHelper http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html so that it can detect database updates and downgrades.

So, the methods

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

and

onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)

can be overridden to delete the old database before creating a new one.

0
source

All Articles